Details and applications of $. Ajax of method parameters in JQuery

  • 2020-03-30 00:53:02
  • OfStack

Url: the address required to send the request for a parameter of type String (the default is the current page address).

Type: requires a parameter of type String, and the request mode (post or get) defaults to get. Notice other HTTP request methods, such as put and

Delete can also be used, but only partially supported by the browser.

Timeout: requires an argument of type Number to set the request timeout time in milliseconds. This setting overrides the global setting of the $.ajaxsetup () method

Buy.

Async: parameter of type Boolean is required, set to true by default, and all requests are asynchronous.

If you need to send a synchronization request, set this option to false. Note that the synchronization request will lock the browser and the user must wait

The request cannot be executed until it is completed.

Cache: requires a Boolean parameter, which defaults to true (or false when the dataType is script).

Setting it to false will not load the request information from the browser cache.

Data: requires parameters of type Object or String to be sent to the server. If it is no longer a string, it is automatically converted to a string lattice

Type. A get request is appended to the url. To prevent this automatic conversion, look at the processData option. The object must be in key/value

For example, {foo1:"bar1",foo2:"bar2"} is converted to &foo1=bar1&foo2=bar2. If it's an array, JQuery will automatically be different

The value corresponds to the same name. For example, {foo:["bar1","bar2"]} converts to &foo=bar1&foo=bar2.

DataType: requires a parameter of type String, the type of data expected to be returned by the server. If not specified, JQuery will automatically mime based on the HTTP package

The information is returned to responseXML or responseText and is passed as an argument to the callback function.

The types available are as follows:

XML: returns an XML document that can be processed with JQuery.

HTML: returns plain HTML information; The included script tags are executed when the DOM is inserted.

Script: returns plain JavaScript code. The results are not automatically cached. Unless the cache parameter is set. Note the remote request

When (not under the same field), all post requests become get requests.

Json: returns json data.

Jsonp: jsonp format. When calling a function in SONP form, such as myurl? The callback =? , JQuery will automatically replace the latter one

"?" Is the correct function name to execute the callback function.

Text: returns a plain text string.

BeforeSend: requires a parameter of type Function, and the Function of the XMLHttpRequest object can be modified before the request is sent, for example by adding a custom

HTTP headers. You can cancel the ajax request by returning false under beforeSend. The XMLHttpRequest object is the only parameter

The number.

The function (XMLHttpRequest) {

This; // the options parameter passed when calling this ajax request

}

Complete: requires an argument of type Function to call the callback Function when the request is complete (called on success or failure).

Parameters: the XMLHttpRequest object and a string describing the type of successful request.

Function (XMLHttpRequest, textStatus) {

This; // the options parameter passed when calling this ajax request

}

Success: requires an argument of type Function, requests a callback Function to be called after success, and takes two arguments.

(1) data returned by the server and processed according to the dataType parameter.

(2) a string describing the state.

Function (data, textStatus) {

//data might be xmlDoc, jsonObj, HTML, text, and so on

This; // the options parameter passed when calling this ajax request

Error: requires an argument of type Function, the Function to be called when the request fails. This function takes three arguments, the XMLHttpRequest object and the error

Error message, captured error object (optional).

The ajax event function is as follows:

Function (XMLHttpRequest, textStatus errorThrown) {

// usually only one of textStatus and errorThrown contains information

This; // the options parameter passed when calling this ajax request

}

ContentType: requires a parameter of type String, and the content encoding type defaults when sending information to the server

As "application/x - WWW - form - urlencoded". This default is suitable for most applications.

DataFilter: a Function that requires a Function type parameter to preprocess the raw data returned by Ajax.

Provide two parameters, data and type. Data is the raw data returned by Ajax, and type is provided when jquery.ajax is called

The dataType parameter. The value returned by the function will be further processed by jQuery.

Function (data type) {

// returns the processed data

Return the data.

}

Global: requires a parameter of type Boolean and defaults to true. Indicates whether a global ajax event is triggered. Setting it to false will not trigger globally

Ajax events, ajaxStart or ajaxStop, can be used to control various ajax events.

IfModified: requires a Boolean parameter of type false by default. Gets new data only when server data changes.

The determination of server data change is based on the last-modified header information. The default value is false, which ignores header information.

Jsonp: requires a parameter of type String to override the name of the callback function in a jsonp request.

This value is used instead in "callback=?" The "callback" part of the URL parameter in this GET or POST request, for example

{jsonp:'onJsonPLoad'} causes "onJsonPLoad=?" To the server.

Username: requires a parameter of type String to be used in response to an authentication request for HTTP access.

Password: requires a parameter of type String to be used in response to an authentication request for HTTP access.

ProcessData: requires a parameter of type Boolean, true by default. By default, the data sent is converted into an object (from a technical point of view)

To match the default content type "application/x-www-form-urlencoded". If you want to send the DOM

For tree information or other information that you do not want to convert, set it to false.

ScriptCharset: requires a parameter of type String, only if the dataType is "jsonp" or "script" at the time of the request, and the type is GET

Will be used to force character set changes (charset). Often, local and remote content encoding are not used together.



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(); //Clear everything in resText

var html = ''; 

$.each(data, function(commentIndex, comment){ 

html += '<div class="comment"><h6>' + comment['username'] 

+ ':</h6><p class="para"' + comment['content'] 

+ '</p></div>'; 

}); 

$('#resText').html(html); 

} 

}); 

}); 

}); 

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

Unlike the JQuery object's each() method, the $.each() function is a global function that does not manipulate the JQuery object, but takes an array or object as the first argument and a callback as the second argument. The callback function takes two arguments: the first is a member of the object or an index of the array, and the second is the corresponding variable or content.

Related articles: