SoFunction
Updated on 2025-04-09

A quick understanding of AJAX in JQuery


$.ajax({
  url:"",  //Requested URL address  dataType:"json",  //Return format is json  async:true,//Whether the request is asynchronous, the default is asynchronous, which is also an important feature of ajax  data:{"id":"value"},  //Particle value  type:"GET",  //Request method  beforeSend:function(){
    //Presponse before request  },
  success:function(req){
    // Process when the request is successful  },
  complete:function(){
    //Request completion processing  },
  error:function(){
    //Request error handling  }
});

Common parameters of ajax method

:

Requires a parameter of type String, (default is the current page address) to send the requested address.

:

Requires a parameter of type String, and the request method (post or get) defaults to get. Note that other http request methods, such as put and delete, can also be used, but only some browsers support it.

:

Requires a Boolean type parameter, set to true by default, and all requests are asynchronous requests. If you need to send a synchronization request, set this option to false. Note that the synchronization request will lock the browser, and other operations of the user must wait for the request to be completed before it can be executed.

:

Requires parameters of type Object or String, data sent to the server. If it is no longer a string, it will be automatically converted to string format. The get request will be appended after the url. To prevent this automatic conversion, you can view the processData option. The object must be in key/value format, e.g.

{foo1:"bar1",foo2:"bar2"}Convert to&foo1=bar1&foo2=bar2. If it is an array, JQuery will automatically correspond to the same name for different values. For example{foo:["bar1","bar2"]}Convert to&foo=bar1&foo=bar2

:

Requires a parameter of type String, the data type expected to be returned by the server. If not specified, JQuery will automatically return responseXML or responseText based on the http package mime information and pass it as a callback function parameter. The available types are as follows:
xml: Returns XML document, which can be processed by JQuery.
html: Returns plain text HTML information; the included script tag will be executed when inserting the DOM.
script: Returns plain text JavaScript code. The results will not be cached automatically. Unless the cache parameter is set. Note that when remote requests (not under the same domain), all post requests will be converted to get requests.
json: Returns JSON data.
jsonp: JSONP format. When calling a function using SONP form, for example, myurl?callback=?, JQuery will automatically replace the next "?" with the correct function name to execute the callback function.
text: Returns a plain text string.

Required as a parameter of Function type. Before sending a request, you can modify the function of the XMLHttpRequest object, such as adding a custom HTTP header. If false is returned in beforeSend, the ajax request can be cancelled. The XMLHttpRequest object is the only parameter.

    function(XMLHttpRequest){
        this;  //The options parameter passed when calling this ajax request      }

Requires a parameter of Function type, and the callback function called after the request is completed (called when the request is successful or failed). Parameters: XMLHttpRequest object and a string describing the type of successful request.

    function(XMLHttpRequest, textStatus){
       this;  //The options parameter passed when calling this ajax request     }

The request is a parameter of Function type. The callback function called after the request is successful has two parameters.

(1) Return the data from the server and processed according to the dataType parameter.
(2) A string describing the status.

 function(data, textStatus){
      //Data may be xmlDoc, jsonObj, html, text, etc.      this; //The options parameter passed when calling this ajax request     }

:

Required as a function type parameter, function called when the request fails. This function has 3 parameters, namely XMLHttpRequest object, error message, and captured error object (optional). The ajax event function is as follows:

function(XMLHttpRequest, textStatus, errorThrown){
     //Usually, only one of the textStatus and errorThrown contains information     this;  //The options parameter passed when calling this ajax request    }

Summarize

The above is the AJAX in JQuery introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!
If you think this article is helpful to you, please reprint it. Please indicate the source, thank you!