Mohamad
Mohamad

Reputation: 35349

What do the arguments inside an ajax function refer to?

I often see this in ajax function function(event, data, status, xhr). I'm wondering what those arguments refer to and how they are used.

Upvotes: 0

Views: 45

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

  • data: The data returned by the server (could be HTML, XML, JSON, ...)
  • status: a string categorizing the status of the request ("success", "notmodified", "error", "timeout", "abort", or "parsererror")
  • xhr: the jqXHR object used to send the AJAX request

In 99.99% of the cases all you care about is the data returned by the server. You might also be interested whether the ajax request succeeded or failed:

$.ajax({
    url: '/somescript.cgi',
    success: function(data) {
        // The request succeeded => do something with the data returned by the server
    },
    error: function() {
        // The request failed
    }
});

Upvotes: 2

Related Questions