Meaning of parameters in an AJAX request

Asked

Viewed 3,547 times

18

When making a request using $.ajax,

  • What is the meaning of processData: false, contentType: false and async: false, what are these fields for?
  • There is difference in using type: "post" or type: "POST"?
  • Why is it used data: JSON.stringify?

    $.ajax({
         url: url,
         type: "POST",
         async: false,
         data: JSON.stringify({ grid: grid }),   
         processData: false,
         contentType: false,
         dataType: "json",
         success: function (data){
                 console.log("Olá");   
         }
    });
    

3 answers

17


"async": false or true

If it’s asynchronous or not. You may notice a difference in this attribute when working with answers on the screen or when you need to trigger a trigger at the very end of the execution, for example. Thus, you trigger another event only at the end of the synchronous execution of the $.ajax. But this sometimes causes the page to freeze.

You can test the application of this using a alert("alguma coisa"); in function success. You’ll find that async: true, the alert will be triggered before having a response from the execution of the ajax, and with the async: false, the alert will only appear when the exact end of the ajax.

There is difference in using type: "post" or type: "POST"?

From my own experience, I never noticed any difference. However, on $.ajax[+] is all in capital letters.

Why date is used: JSON.stringify?

It depends on how the server is waiting for the answer of the execution. For a simple sending of post for example, just one data: $("form").serialize().

For sending a JSON to the server, then use the JSON.stringify combined with the contentType: "application/json".

  • 1

    The async determines asynchrony of the method ajax for the rest of the code, not for callback success. The success only occurs when the request is successful, i.e., when whatever you are requesting is available, the async determines whether the remnant of its logic awaits (or not) this success. See this discussion

9

I think your own answer already answers some of your questions.

  • JSON.stringify: simply serves to convert the JSON object to a format that can be transferred via an HTTP request.

  • contenttype: false: contenttype header will not be placed in the HTTP request. It is always good to specify the type of data being sent - in your case application/json'

  • types 'post' and 'POST'': the default is to send with uppercase letters. But the request will convert automatically if you send with lowercase letters. That is, it makes no difference. (See W3.org item 4.6.1)

  • async: basically, by running asynchronously, the rest of the code will not wait for the server’s response to proceed. If async: false, the rest of the code will only run after the server’s response.

8

In the documentation (in English), has the meaning of the parameters.

  • async: By default, all requests are sent asynchronously, defined with true. If you need synchronous requests, set this option to false . Cross-Omain requests and dataType: "jsonp" do not support synchronous operation. Note that synchronous requests can temporarily block the browser, disabling any actions while the request is active.

  • beforeSend: To change the request header or perform operations before sending the request, use this function.

  • cache: If defined as false, it will force pages not to be cached by the requested browser.

  • complete: The function to be called when the request finishes (after success and error call returns are executed).

  • contents: A string/regular expression object that determines how jQuery will parse the answer, given its content type.

  • contentType: When sending data to the server, use this type of content. The default is "application/x-www-form-urlencoded; charset=UTF-8", which is good for most cases. If you explicitly pass on a type of content to $.ajax(), then it is always sent to the server (even if no data is sent).

  • context: This object will be the context of all call returns related to Ajax. By default, the context is an object that represents the Ajax settings used in the call ($.ajaxSettings merged with the definitions passed to $.ajax).

  • converters: An object containing the converters dataType-to-dataType. The value of each converter is a function that returns the transformed value of the response.

  • crossDomain: If you want to force a request crossdomain (as JSONP) in the same domain, set the value of crossdomain to true. This allows, for example, redirecting the server to another domain.

  • data: Data to be sent to the server. The format should be variable/value and if you do not leave in this format the function will format automatically.

  • dataFilter: A function to be used to manipulate raw response data from XMLHttpRequest. This is a pre-filtering function to sanitize the answer. You must return the sanitized data. The function takes two arguments: Raw data returned from the server and the parameter dataType.

  • dataType: You define the type of data to be returned by the request. Possible values are: xml, html, json, jsonp, script and text. Configuring this option allows an evaluation of the data that is returned. If this option is omitted the function identifies the data type but does not evaluate.

  • error: A function to be called if the request fails. The function takes three arguments: The jqXHR object (in jQuery 1.4.x, Xmlhttprequest), a string describing the type of error that occurred and an optional exception object, if it occurred.

  • global: Specifies whether the manipulative events defined in .ajaxSend() and .ajaxError() will apply to the current request. By default you are set to true.

  • headers: An additional header object to send along with requests using the Xmlhttprequest transport.

  • ifModified: Allow the request to succeed only if the response has changed since the last request. This is done by checking the header Last-Modified. The default value is false , ignoring the header.

  • isLocal: Allows the current environment to be recognized as "local" (for example, the file system), even if jQuery does not recognize it as such by default. The following protocols are currently recognized as local: file , *-extension and widget . If the isLocal precise definition of modification, it is recommended to do so once in the $.ajaxSetup() method.

  • jsonp: Replaces the function name callback in a request JSONP.

  • jsonpCallback: Specifies the function name callback for a request JSONP. This value will be used instead of the random name generated automatically by jQuery.

  • method: The HTTP method to be used for the request (for example, POST, GET, PUT).

  • mimeType: A mime type to replace the mime type XHR.

  • password: A password to be used with Xmlhttprequest in response to an HTTP access authentication request.

  • processData: This variable is configured to process and transform the information passed to the default application/x-www-form-urlencoded. If for some reason you do not want this formatting, such as sending in xml format to the server, assign false for this variable.

  • scriptCharset: Applies only when "script" transport is used (for example, requests between domains with data type jsonp or script and kind GET). Defines the charset attribute in the script tag used in the request. Used when the character set on the local page is not the same as in the remote script.

  • statusCode: A code object and HTTP numeric functions to be called when the response has the corresponding code.

  • success: If the request is successfully completed, this function is executed, which receives the data received from the server.

  • timeout: If you want to limit the time for executing the request, set a value in milliseconds. Not returning success within set time, an error message, configured by you in the function error, will be shown.

  • traditional: Set this option to true if you want to use the traditional style of parameter serialization.

  • type: The method used for the request. Accepts GET or POST. The standard is GET.

  • url: Address on the server that will receive the request.

  • username: A user name to be used with XMLHttpRequest in response to an HTTP access authentication request.

  • xhr: Callback to create the object XMLHttpRequest.

  • xhrFields: An object of pairs fieldName-fieldValue to set about the native XHR object.

Information based on Answer accepted:

There is difference in using type: "post" or type: "POST"?

Not.

Why date is used: JSON.stringify?

It depends on how the server is waiting for the answer of the execution. For a simple post submission for example, a date is enough: $("form").serialize().

To send a JSON to the server, it uses the JSON.stringify combined with the contentType: "application/json".

The JSON.stringify serves to transform an object with guys primitive in a string to be sent in string-like in a universal JSON format.

  • 3

    Relevant canonical reference: http://api.jquery.com/jQuery.ajax

Browser other questions tagged

You are not signed in. Login or sign up in order to post.