user7031 user7031 - 6 months ago 16
Ajax Question

Jquery ajax post doesn't send data to server end(Using PHP)

HTML

<a id="1">Add to notebook</a>
<a id="2">Add to notebook</a>
<a id="3">Add to notebook</a>
<a id="4">Add to notebook</a>
...
<a id="40">Add to notebook</a>


JavaScript

<script>
$(document).ready(function(){
//callback handler for post submit
$("a").click(function(e)
{
var vocab_id = $(this).attr("id");
var formURL = 'http://localhost/test/test.php';
$.ajax(
{
url : formURL,
type: "POST",
data : vocab_id,
dataType : "text",
});
e.preventDefault(); //STOP default action
//e.unbind(); //unbind. to stop multiple form submit.
});
});
</script>


I have a list of hyperlinks, it will send the currently clicked value of id to the server end by clicking the respective link.

But the problem is


  1. Back-end doesn't receive any post data at all.

  2. People said that I should uncomment the e.unbind() to stop multiple submit, but I got error after that line was uncommented. Error message is like this.



Uncaught TypeError: undefined is not a function 2:479(anonymous function) 2:479n.event.dispatch jquery-1.11.0.js:3r.handle

Answer

The data part in jQuery.ajax() should be like either

data :{ name : vocab_id } 

or

data : "name="+vocab_id

Syntax for unbind() is not correct use the following

$(this).unbind(e);

it will unbind the object from current event handler

Comments