Luka
Luka

Reputation: 69

What are "succes" and "error" in a jQuery ajax call?

I am writing papper about JavaScript and if url, method and data are arguments that method recives then what about success and error? are they arguments or are they called something else?

$.ajax({
    url: "url",
    method: "get",
    data: {
       "someData":someData
    },
    success: function (data) {         
         alert(data);
    },
    error: function (errorThrown) {
        alert(errorThrown);
    }
});

Upvotes: 2

Views: 574

Answers (6)

Abhijit
Abhijit

Reputation: 252

success and error are callback handlers.success() is the success handler which is called when the server returns a valid response (200 status code) and error() is the error handler which is called when the server fails to respond to a valid request(500 response code)

Upvotes: 0

Akshay Hegde
Akshay Hegde

Reputation: 16997

Both success and error are callback functions,

In short success and error are to specify what to do in case of success or failure of the request respectively.

From Jquery API

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.

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.

So how it works ?

AJAX communicates with the server using XMLHttpRequest object

enter image description here

  1. User sends a request from the UI and a javascript call goes to XMLHttpRequest object.

  2. HTTP Request is sent to the server by XMLHttpRequest object.

  3. Server interacts with the database using JSP, PHP, Servlet, ASP.net etc.

  4. Data is retrieved.

  5. Server sends XML data or JSON data to the XMLHttpRequest callback function.

    • 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

  6. HTML and CSS data is displayed on the browser.

Upvotes: 3

clabe45
clabe45

Reputation: 2454

$.ajax takes an argument of options, which is a generic object. success and error are callback functions that are members of the options object. So, you could call them many things. I would refer to them as callback functions.

Upvotes: 0

Alaksandar Jesus Gene
Alaksandar Jesus Gene

Reputation: 6887

When you make a backend call after the page is loaded in browser its known as AJAX (Asynchronous Javascript). This method will fetch the data from the server without reloading the page.

Now as you are making a server call, you will be facing two scenarios

Success = your call is successful and server is giving you the requested data.

Error = your call has failed due to two major reasons

2a. Either your url is wrong or the url you specified is not defined to match the backend server. In this case, you get 404 error

2b. Your url is correct, but you are not passing the right parameters for the server to respond or you are passing some data which is not required by that url

For example, for a login verification you need to pass username and password. Instead if you pass as email and password, it will throw error.

Upvotes: 0

mahip_j
mahip_j

Reputation: 368

They are callback functions. When ajax call , in your case get call, successfully completed then success() function will be called and particular functionality inside will be get executed and if any error is there then error() function will be called and get executed

Upvotes: 1

jmona789
jmona789

Reputation: 2839

They are also arguments, but could also be called callback functions

Upvotes: 1

Related Questions