Ajax Success Event Not Working

Ajax success event not working

The result is probably not in JSON format, so when jQuery tries to parse it as such, it fails. You can catch the error with error: callback function.

You don't seem to need JSON in that function anyways, so you can also take out the dataType: 'json' row.

Why does my ajax success callback-function not work as expected?

From the ajax events docs:

success (Local Event)

This event is only called if the request was successful (no errors from the server, no errors with the data).

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):

dataType

The type of data that you're expecting back from the server.

The available types (and the result passed as the first argument to your success callback) are:

"json": Evaluates the response as JSON and returns a JavaScript object.
...
The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

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:

$("#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');
},
error: function(err) {
console.log(err.responseText); // <-- printing error message to console
}
});
});

Jquery: Ajax POST request / success event not fired

The result is probably not in JSON format, so when jQuery tries to parse it as such, it fails.
You can catch the error with error: callback function.
Either you can return the response in json format from your server or remove dataType:'JSON' from your AJAX call.

Codeigniter with ajax: success function not working

I find the solution is because of CSRF security

$("#country select").change(function () { 
var country_value= $(this).val();
var data = { /* params */
"country": country_value,
"state": '001'
};
data[csfr_token_name] = $.cookie(csfr_cookie_name);
$.ajax({
url:base_url + "Search_controller/testing_controller",
method: 'post',
data: data,
dataType: 'json',
success: function(data){
console.log('done : ' + data);
},
error: function (reponse) {
console.log('Problem with ajax');
}

});

This Code Working



Related Topics



Leave a reply



Submit