I use the following AJAX
call in order to make CRUD
operations in MVC
. However, as there are many different usage of complete, success, error functions of AJAX calls, I want to be clarified want is the most common or suitable usage of them. In some examples, there is no parameter for these functions but in other examples there are several parameters (i.e. jqXHR
, textStatus
, errorThrown
). Of course I use the necessary operation for each case i.e. displaying notification or confirmation messages, etc. but I just need to the default or standart usage each of them. Could you please a brief explanations and post the most suitable way for standard usage of AJAX
for CRUD
operations?
$.ajax({
type: "POST",
url: '@Url.Action("Insert", "Account")',
cache: false,
dataType: "json",
data: formdata,
complete: function () {
//???
},
success: function (data, textStatus, XMLHttpRequest) {
// ???
},
error: function (response) {
// ???
},
//other example usages of error function :::
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
},
error: function (xhr) {
console.log('Error: ' + xhr.statusText);
}
});
You'll see different examples of these because:
I would recommend to always base your use of .ajax in accordance to documentation for the version that you are using. So if you're looking for a standard way to use .ajax for your application, your best bet is to follow the documentation syntax:
http://api.jquery.com/jquery.ajax/
The documentation is very good at describing what changes came into play for what versions of jQuery, so by paying attention to that you can avoid problems in your application.
From the jQuery documentation:
complete
Type: Function( jqXHR jqXHR, String textStatus )
A function to be called when the request finishes (after success and error callbacks are executed). The function gets passed two arguments: The jqXHR (in jQuery 1.4.x, XMLHTTPRequest) object and a string categorizing the status of the request ("success", "notmodified", "nocontent", "error", "timeout", "abort", or "parsererror"). As of jQuery 1.5, the complete setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
success
Type: Function( Anything data, String textStatus, jqXHR jqXHR )
A function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter or the dataFilter callback function, if specified; a string describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery 1.5, the success setting can accept an array of functions. Each function will be called in turn. This is an Ajax Event.
error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments