How should be a standard AJAX call with complete, success and error section

Jack

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);
    }
}); 
Robert Wade

You'll see different examples of these because:

  • Some of the arguments are optional or mixed type
  • Some of it, just depends on what the author decided to name the arguments in the callback functions.
  • Some use of these callbacks and their arguments depend on the jQuery version the author was using at the time.

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JSONP - Trigger Complete, Success, Error Ajax call

From Dev

Processing an AJAX call in Rails 4 (success, error, beforeSend, complete)

From Dev

Processing an AJAX call in Rails 4 (success, error, beforeSend, complete)

From Dev

ajax error results in success function call

From Java

jQuery ajax() using success, error and complete vs .done(), .fail() and always()

From Dev

How to repeat ajax call until success

From Dev

How to navigate between divs On Success in ajax call

From Dev

how to show hidden div on ajax success call

From Dev

How to repeat ajax call until success

From Dev

How should one handle AJAX success/error responses in Clojure re-frame?

From Dev

jQuery ajax call fires error event on a 200 success call

From Dev

HTML5 geolocation: How to get pass lat and long values to an AJAX call after a success or error callback

From Dev

Ajax success call is not executing

From Dev

Ajax call on success not working

From Dev

How to Execute the function after Ajax call is complete

From Dev

ajax success error function doesn't call up in jquery

From Dev

Ajax call fires success event but returns 201 error

From Dev

Ajax success and error

From Dev

Returning AJAX Success and Error

From Dev

AJAX Success or error not firing

From Dev

AJAX error is returned as Success

From Dev

Flask, how to return a success status code for ajax call

From Dev

how to call spring controller in jquery ajax success method

From Dev

How to call external method from nested ajax (success) method?

From Dev

How to return a List from controller to Ajax call and print it in the success?

From Dev

Ajax request, how to call an other function than "success" one's?

From Dev

how to call spring controller in jquery ajax success method

From Dev

How to use variable data after Ajax Call Success - Laravel

From Dev

Make method asynchronius in ajax success section

Related Related

  1. 1

    JSONP - Trigger Complete, Success, Error Ajax call

  2. 2

    Processing an AJAX call in Rails 4 (success, error, beforeSend, complete)

  3. 3

    Processing an AJAX call in Rails 4 (success, error, beforeSend, complete)

  4. 4

    ajax error results in success function call

  5. 5

    jQuery ajax() using success, error and complete vs .done(), .fail() and always()

  6. 6

    How to repeat ajax call until success

  7. 7

    How to navigate between divs On Success in ajax call

  8. 8

    how to show hidden div on ajax success call

  9. 9

    How to repeat ajax call until success

  10. 10

    How should one handle AJAX success/error responses in Clojure re-frame?

  11. 11

    jQuery ajax call fires error event on a 200 success call

  12. 12

    HTML5 geolocation: How to get pass lat and long values to an AJAX call after a success or error callback

  13. 13

    Ajax success call is not executing

  14. 14

    Ajax call on success not working

  15. 15

    How to Execute the function after Ajax call is complete

  16. 16

    ajax success error function doesn't call up in jquery

  17. 17

    Ajax call fires success event but returns 201 error

  18. 18

    Ajax success and error

  19. 19

    Returning AJAX Success and Error

  20. 20

    AJAX Success or error not firing

  21. 21

    AJAX error is returned as Success

  22. 22

    Flask, how to return a success status code for ajax call

  23. 23

    how to call spring controller in jquery ajax success method

  24. 24

    How to call external method from nested ajax (success) method?

  25. 25

    How to return a List from controller to Ajax call and print it in the success?

  26. 26

    Ajax request, how to call an other function than "success" one's?

  27. 27

    how to call spring controller in jquery ajax success method

  28. 28

    How to use variable data after Ajax Call Success - Laravel

  29. 29

    Make method asynchronius in ajax success section

HotTag

Archive