ajax parameter details


Recently took a little time to brush up on ajax used in the project, more systematic understanding of the parameters ajax, where the content is bold common parameters, for everyone to share.

url

Requirements for the type String parameter (the default address of this page) address of the request.

type

String parameter type requirements, request method (POST or get) defaults to get. Note that other http request method, such as put and delete can also be used, but only part of the browser supports.

timeout

Number parameter type requirements, set request time (in milliseconds). This setting overrides $ .ajaxSetup () method of the global settings.

async

Requirements Boolean type parameter defaults to true, 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, the user must wait for another request to complete before the operation can be performed.

cache

Boolean type parameter is required, the default is true (as when dataType Script, the default is false), is set to false information will not load request from the browser cache.

data

Object type String requirements or parameters, data sent to the server. If you have not a string, it will automatically be converted to string format. get request will be appended to the url. Prevent this automatic conversion, you can view processData options. Object must key / value format, e.g. {foo1: "bar1", foo2: "bar2"} is converted to & foo1 = bar1 & foo2 = bar2. If the array, JQuery will automatically correspond to the same name to different values. For example {foo: [ "bar1", "bar2"]} is converted to & foo = bar1 & foo = bar2.

dataType

Requirements for the parameter of type String, the expected data type returned by the server. If not specified, will automatically return JQuery responseXML or http responseText The mime information packet, and the callback function passed as parameters. Available types are as follows:
xml: returns an XML document, available JQuery process.
html: Returns the HTML plain text information; script tag contained performs when inserted DOM.
script: return plain text JavaScript code. It does not automatically cached results. Unless the cache parameter sets. Note that when the remote request (not in the same domain under), all post requests are converted to get the request.
json: returns JSON data.
jsonp: JSONP format. When using the form SONP call functions, such as myurl? Callback = ?, JQuery will automatically replaced after a "?" Is the correct name of the function to execute the callback function.
text: Returns the plain text string.

beforeSend

Function type parameter is required, the function may be modified before sending a request XMLHttpRequest object, such as adding custom HTTP header. Returns false if the beforeSend can cancel the ajax request. The XMLHttpRequest object is the only parameter.
function (the XMLHttpRequest) {
the this; // Options parameter passed when calling this ajax request
}

complete

Function requirements for the type of parameters, request a callback function to call when completed (requests are called when the success or failure). Parameters: XMLHttpRequest object and a type description string successful request.
function (the XMLHttpRequest, textStatus) {
the this; // Options parameter passed when calling this ajax request
}

success

Function requirements for the type of parameters, the callback function to call when the request is successful, there are two parameters.
(1) returned by the server, and performs data processing in accordance with the dataType parameter.
String (2) described state.
function (Data, textStatus) {
// Data may be xmlDoc, jsonObj, html, text like
this; options // call parameters passed when this ajax request
}

error

Function type parameter is required, the function is called when the request fails. This function takes three parameters, i.e., XMLHttpRequest object, an error message, error trapping object (optional). ajax event function is as follows:
function (the XMLHttpRequest, textStatus, errorThrown) {
// textStatus and usually contains only one information errorThrown
this; options // call parameters passed when this ajax request
}

contentType

String parameter type requirements, when transmission information to the server, the content default encoding type "application / x-www-form-urlencoded". The default value is suitable for most applications.

data filter

Function type parameter requirements, to the raw data returned by the function Ajax pretreatment. Provide data and type two parameters. Ajax data is raw data returned, type dataType parameter is provided when calling jQuery.ajax. The value returned by jQuery further processing.
function (Data, type) {
// return the processed data
return Data;
}

data filter

Function type parameter requirements, to the raw data returned by the function Ajax pretreatment. Provide data and type two parameters. Ajax data is raw data returned, type dataType parameter is provided when calling jQuery.ajax. The value returned by jQuery further processing.
function (Data, type) {
// return the processed data
return Data;
}

global

Requirements for the type Boolean parameter, the default is true. It indicates whether to trigger a global ajax event. Set to false will not trigger a global ajax event, ajaxStart or ajaxStop ajax be used to control a variety of events.

ifModified

Requirements for the type Boolean parameter, the default is false. To obtain new data only when the server data changes. Server data change judgment based on the Last-Modified header information. The default value is false, ignoring the header information.

jsonp

Requirements parameter of type String, rewrite in the name of a callback function jsonp request. "? Callback =" This value is used to replace the GET or POST request in which the URL parameters in the "callback" section, e.g. {jsonp: 'onJsonPLoad'} will cause "onJsonPLoad =?" To the server.

username

String parameter type requirements, in response to the user name for HTTP access authentication request.

password

String parameter type requirements, in response to a password authentication HTTP access request.

processData

Requirements for the type Boolean parameter, the default is true. By default, data transmission will be converted to an object (from a technical point of view is not a string) to match the default content type "application / x-www-form-urlencoded". If you want to send a DOM tree information or other undesirable information conversion, set to false.

scriptCharset

String parameter type requirements, only when the request is dataType "jsonp" or "script", and GET is only type for forcibly modifying the character set (charset). Not normally used in both local and remote content encoding.

Code

$.ajax({
    type: "GET",
    url: "test.json",
    data: {username: $("#username").val(), content: $("#content").val()},
    dataType: "json",
    success: function (data) {
        $('#resText').empty();   //清空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);
    }
});

Here Insert Picture Description

Published 40 original articles · won praise 94 · views 9582

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/102961137