I code this ajax request but I don’t know why the code in the success method doesn’t work
Even though in the networks in chrome browser appear state: 200ok
this is ajax code:
$("#noti_filter").click(function(){
//first add item into cart
var item_id = 'test';
$.ajax({
method:"POST",
//contentType:"application/json",
url:"../html/notifies.php",
data:{product_id:item_id},
dataType: "json",
success:function(data,state) {
console.log(data);
console.log(state);
alert('ajax success');
}
});
});
the problem is that alert or console Not to mention the others code
success:function(data,state)
{
console.log(data);
console.log(state);
alert('ajax success');
}
2
Answers
You defined the dataType as
json
. ThedataType
is the type of data that you’re expecting back from the server. Does your server respondsjson
?I assume the result of your 200-ok-server-request is probably not in
json
format, so the parsing fails and yoursuccess
-callback is not called. You can catch the error witherror
callback function.After that you know the exact reason.
From the ajax events docs:
Since your server responded with
200 OK
that means we can route out problems with the server and are left with errors with the data.From the ajax docs (only the relevant parts):
So most likely the data returned by the server is being rejected by ajax in which case a parse error should be thrown.
This would be an example of how to implement an error handler: