I am apologize for the stupid question, but I need your help. I need to get information about response inside AJAX
.
$.ajax({
type: "POST",
url: '/register',
data : registerRequestJSON,
contentType:"application/json",
success: function(data){
$("#register_area").text();// need to show success
},
error: function(err) {
$("#register_area").text("@text"); // @text = response error, it is will be errors: 324, 500, 404 or anythings else
}
});
How can I use response body? (documentation Jquary.Ajax is not working at the momment)
In most cases, you will need to specify the success and error callbacks. The success callback will be called after the successful completion of the AJAX call. The response returned by the server will be passed along to the success callback.
You can store your promise, you can pass it around, you can use it as an argument in function calls and you can return it from functions, but when you finally want to use your data that is returned by the AJAX call, you have to do it like this: promise. success(function (data) { alert(data); });
Example# success and Error : A success callback that gets invoked upon successful completion of an Ajax request. A failure callback that gets invoked in case there is any error while making the request.
The first param to error handler is jqxhr
, it has the property responseText
which will give the response body.
$.ajax({
type: "POST",
url: '/register',
data : registerRequestJSON,
contentType:"application/json",
success: function(data){
$("#register_area").text();// need to show success
},
error: function(jqxhr) {
$("#register_area").text(jqxhr.responseText); // @text = response error, it is will be errors: 324, 500, 404 or anythings else
}
});
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