ajax({ type: "POST", url: 'test. php', data: {"type":"check"}, success: function(response){ alert(response); } }); There can obviously be more key-val pairs in data. In this case your alert should read: "The type you posted is check".
jQuery ajax() Method The ajax() method is used to perform an AJAX (asynchronous HTTP) request. All jQuery AJAX methods use the ajax() method. This method is mostly used for requests where the other methods cannot be used.
The best way to bubble that error from the server side (using php) to the client side is to send a header through the Ajax request somewhere in the 400's (which is always associated with errors). Once the Ajax request receives this it will trigger your error function.
The A in Ajax stands for asynchronous. That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, $. ajax returns immediately and the next statement, return result; , is executed before the function you passed as success callback was even called.
According to http://api.jquery.com/jQuery.ajax/ the fail
callback should be getting:
jqXHR, textStatus, errorThrown
same as error
, but error
is deprecated:
Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks will be deprecated in jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
Here an example after looking for the same problem:
this.GetOrderList = function (customerId) {
var self = this;
$.post('MySuperServer.aspx', { customerId: customerId })
.done(function (dataStr) {
var orderList = jQuery.parseJSON(dataStr);
self.process(orderList);
})
.fail(function (jqXHR, textStatus, error) {
console.log("Post error: " + error);
});
}
While debugging, I've got:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With