Home>

Ajax is "asynchronous javascript and xml" (asynchronous javascript and xml), which refers to a web development technology for creating interactive web applications.This article introduces the $.ajax () method parameters in jquery.Let's study together

The full name is "asynchronous javascript and xml" (asynchronous javascript and xml);refers to a web development technology to create interactive web applications.

ajax method parameters in jquery can't always remember,Take a note here.

1.url:

Requires a parameter of type string,(Default is the current page address) The address to send the request.

2.type:

Requires a parameter of type string,The request method (post or get) defaults to get. Note the other http request methods,For example put and delete can also be used,But only some browsers support it.

3.timeout:

Requires a parameter of type number,Set the request timeout in milliseconds. This setting will override the global setting of the $.ajaxsetup () method.

4.async:

Requires a boolean parameter,The default setting is true, and all requests are asynchronous.If you need to send a synchronization request,Please set this option to false. Note that synchronization requests will lock the browser,The user's other operations must wait for the request to complete before they can be performed.

5.cache:

Requires a boolean parameter,The default is true (when the datatype is script, the default is false). Setting to false will not load the request information from the browser cache.

6.data:

Requires a parameter of type object or string,Data sent to the server.If it is no longer a string,Will be automatically converted to string format.The get request will be appended to the url. Prevent this automatic conversion,You can view the processdata option. The object must be in key/value format, for example {foo1:"bar1", foo2:"bar2"} is converted 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"]} is converted to&foo=bar1&foo=bar2.

7.datatype:

Requires a parameter of type string,The type of data expected from the server.If not specified,jquery will automatically return responsexml or responsetext according to the http package mime information and pass it as a callback function parameter.The available types are:

xml:returns the xml document, which can be processed by jquery.

html:Returns plain text html information;the included script tag will be executed when the dom is inserted.

script:Returns plain text javascript code. Results are not cached automatically.Unless the cache parameter is set. Note that in remote requests (not in 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,For example, myurl?callback =? ;, jquery will automatically replace the "?" with the correct function nameTo execute the callback function.

text:Returns a plain text string.

8.beforesend:

Requires a parameter of type function,You can modify the function of the xmlhttprequest object before sending a request.For example add a custom http header. If you return false in beforesend, you can cancel this Ajax request. The xmlhttprequest object is the only parameter.

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

9.complete:

Requires a parameter of type function,Callback function called after the request is completed (called when the request succeeds or fails). Parameters:xmlhttprequest object and a string describing the type of successful request.

function (xmlhttprequest, textstatus) {
       this;//options parameter passed when calling this ajax request
     }

10.success:

Requires a parameter of type function,Callback function called after the request is successful,There are two parameters.

(1) returned by the server,And processed data according to the datatype parameter.

(2) A character string describing the status.

function (data, textstatus) {
      //data may be xmldoc, jsonobj, html, text, etc.
      this;//options parameter passed when calling this ajax request
     }

11.error:

Requires a parameter of type function,The function to be called when the request fails.This function has 3 parameters, namely xmlhttprequest object, error information, and captured error object (optional).

The ajax event function is as follows:

 function (xmlhttprequest, textstatus, errorthrown) {
     //Normally only one of textstatus and errorrown contains information
     this;//options parameter passed when calling this ajax request
    }

12.contenttype:

Requires a parameter of type string,When sending information to the server,The content encoding type is "application/x-www-form-urlencoded" by default. This default value is suitable for most applications.

13.datafilter:

Requires a parameter of type function,A function that preprocesses the raw data returned by ajax.Provide data and type two parameters.data is the raw data returned by ajax,type is the datatype parameter provided when calling jquery.ajax. The value returned by the function will be further processed by jquery.

 function (data, type) {
        //Return the processed data
        return data;
      }

14.datafilter:

Requires a parameter of type function,A function that preprocesses the raw data returned by ajax.Provide data and type two parameters.data is the raw data returned by ajax,type is the datatype parameter provided when calling jquery.ajax. The value returned by the function will be further processed by jquery.

function (data, type) {
        //Return the processed data
        return data;
      }

15.global:

Requires a boolean parameter,The default is true. Indicates whether to trigger global ajax events. Setting to false will not trigger global ajax events, ajaxstart or ajaxstop can be used to control various ajax events.

16.ifmodified:

Requires a boolean parameter,The default is false. Get new data only when server data changes.The basis of the server data change judgment is the last-modified header information. The default value is false, which means that header information is ignored.

17.jsonp:

Requires a parameter of type string,Rewrite the name of the callback function in a jsonp request.This value is used to replace the "callback" part of the url parameter in a "callback =?" such a get or post request. For example, {jsonp:"onjsonpload"} will cause "onjsonpload =?" To the server.

18.username:

Requires a parameter of type string,The user name used to respond to the HTTP access authentication request.

19.password:

Requires a parameter of type string,The password used to respond to the HTTP access authentication request.

20.processdata:

Requires a boolean parameter,The default is true. by default,The data sent will be converted to an object (not technically a string) to match the default content type "application/x-www-form-urlencoded". If i want to send dom tree information or other information that you do not want to convert,Please set to false.

21.scriptcharset:

Requires a parameter of type string,Only when the datatype is "jsonp" or "script" when requested and the type is get will it be used to force modification of the charset. Usually used when local and remote content encodings are not the same.

Case code:

$(function () {
  $("#send"). click (function () {
     $.ajax ({
       type:"get",       url:"test.json",       data:{username:$("#username"). val (), content:$("#content"). val ()},       datatype:"json",       success:function (data) {
             $("#restext"). empty ();//Empty all content in restext
             var html="";
             $.each (data, function (commentindex, comment) {
                html +="<div><h6>" + comment ["username"]
                     + ":</h6><p" + comment ["content"]
                     + "</p></div>";
             });
             $("#restext"). html (html);
           }
     });
  });
});

22. By the way, the $.each () function:

The $.each () function is different from the each () method of the jquery object. It is a global function.Does not operate the jquery object, but takes an array or object as the first parameter, and a callback function as the second parameter. The callback function has two parameters:the first is the index of a member or array of the object,The second is the corresponding variable or content.

  • Previous A complete example of an image viewer implemented in Java
  • Next Application of PHP + jQuery + Ajax to achieve pagination effect jPaginate plugin