Reputation: 153
I have the following jQuery code, but it's not running the alert.
Can anyone tell me what's wrong?
jQuery('.list_action .edit').click(function(){
var answerId = jQuery(this).parent().next('#answer_id').val();
jQuery.ajax({
type: 'POST',
url : 'form.php',
data: {'answer_id':answerId},
dataType : 'json',
}).done(function(result){
if(result == true){
alert('success');
}else{
alert('faild');
}
});
});
in file php i have code
echo $response = json_encode($answersAjax->getData());
Many thank!
Upvotes: 1
Views: 2356
Reputation: 196
Try to see if an error was returned from the server by including "error: function" in the call:
var data = "answer_id="+answer_id;
req = $.ajax({
type: 'POST',
encoding:"UTF-8",
url: 'index.php',
cache: false,
data: data,
error: function (xhr, ajaxOptions, thrownError) {
//alert(xhr.responseText);
alert("Error: "+thrownError);
},
xhr: function () {
var xhr = new window.XMLHttpRequest();
return xhr;
},
beforeSend: function () {
//do sth
},
complete: function () {
//do sth
},
success: function (response) {
alert("success");
}
});
If you have more than one variable, then use
var data = "var1="+var1+"&var2="+var2+"&var3="+var3
If you still don't get anything back, then your url is not correct. Did you try to output some text in your server code, to see if you received the request at all? E.g.:
echo "answer id=".$_POST['answer_id'];
If you don't see at least "answer id=" then you know that the URL is certainly not correct!
Upvotes: 3