Home > Web Front-end > JS Tutorial > Detailed explanation and application of $.ajax() method parameters in JQuery_jquery

Detailed explanation and application of $.ajax() method parameters in JQuery_jquery

WBOY
Release: 2016-05-16 17:09:20
Original
906 people have browsed it

url: requires a parameter of String type, (default is the current page address) the address to which the request is sent.

type: The parameter is required to be of String type. 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 are only supported by some browsers.

timeout: The requirement is a Number type parameter, set the request timeout (milliseconds). This setting will override the global setting

of the $.ajaxSetup() method.

async: The requirement is a Boolean type parameter. The default setting is true. All requests are asynchronous requests.

Set this option to false if you need to send synchronous requests. Note that synchronous requests will lock the browser, and other user operations must wait

for the request to be completed before they can be executed.

cache: requires a parameter of Boolean type, 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.

data: Requires parameters of type Object or String, data sent to the server. If it is not a string, it will be automatically converted into the string format

. The get request will be appended to the url. To prevent this automatic conversion, check the processData option. The object must be in the key/value format

. For example, {foo1:"bar1",foo2:"bar2"} is converted to &foo1=bar1&foo2=bar2. If it is an array, JQuery will automatically assign the same name to different

values. For example, {foo:["bar1","bar2"]} is converted to &foo=bar1&foo=bar2.

dataType: Parameters required to be String type, 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 an XML document that can be processed with JQuery.

html: Returns plain text HTML information; the included script tag will be executed when inserted into the DOM.

script: Returns plain text JavaScript code. Results are not cached automatically. Unless cache parameters are set. Note that when requesting

remotely (not under the same domain), all post requests will be converted into get requests.

json: Returns JSON data.

jsonp: JSONP format. When calling a function using the SONP form, such as myurl?callback=?, JQuery will automatically replace the last

“?” with the correct function name to execute the callback function.

text: Returns a plain text string.

beforeSend: The parameter is required to be Function type. You can modify the function of the XMLHttpRequest object before sending the request, such as adding a custom

HTTP header. If false is returned in beforeSend, this ajax request can be canceled. The XMLHttpRequest object is the only parameter

.

function(XMLHttpRequest){

this; //The options parameter passed when calling this ajax request

}

complete: required to be Function type parameter, the callback function called after the request is completed (called when the request succeeds or fails).

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

function(XMLHttpRequest, textStatus){

this; //The options parameters passed when calling this ajax request

}

success: The requirement is Function type parameter, the callback function called after the request is successful, has two parameters.

(1) Data returned by 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; //passed when calling this ajax request The options parameter

error: requires a parameter of Function type, a function that is called when the request fails. This function has 3 parameters, namely XMLHttpRequest object, error

error message, and captured error object (optional).

ajax event function is as follows:

function(XMLHttpRequest, textStatus, errorThrown){

//Normally only one of textStatus and errorThrown contains information

this; //The options parameter passed when calling this ajax request

}

contentType: requires a parameter of String type. When sending information to the server, the content encoding type defaults

is "application/x-www-form-urlencoded". This default value is suitable for most applications.

dataFilter: requires parameters of Function type, a function that preprocesses the original data returned by Ajax.

Provide two parameters: data and type. data is the original data returned by Ajax, and 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 processed data

return data;

}

global: Requires parameters of Boolean type, defaults to true. Indicates whether to trigger the global ajax event. Setting to false will not trigger the global

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

ifModified: requires a Boolean type parameter, the default is false. Only get new data when server data changes.

The basis for judging server data changes is the Last-Modified header information. The default value is false, which means header information is ignored.

jsonp: The parameter is required to be String type, and the name of the callback function is rewritten in a jsonp request.

이 값은 "callback=?"과 같은 GET 또는 POST 요청에서 URL 매개변수의 "콜백" 부분을 대체하는 데 사용됩니다(예:

{jsonp:'onJsonPLoad'). } "onJsonPLoad=?"가 서버로 전달됩니다.

username: HTTP 액세스 인증 요청의 사용자 이름에 응답하는 데 사용되는 문자열 유형의 매개변수가 필요합니다.

password: HTTP 액세스 인증 요청에 응답하는 데 사용되는 비밀번호인 문자열 유형 매개변수가 필요합니다.

processData: 부울 유형 매개변수가 필요하며 기본값은 true입니다. 기본적으로 전송된 데이터는 기본 콘텐츠 유형 "application/x-www-form-urlencoded"와 일치하도록 객체(기술적으로는 문자열이 아닌

)로 변환됩니다. DOM

트리 정보나 기타 변환하고 싶지 않은 정보를 전송하고 싶다면 false로 설정해주세요.

scriptCharset: 요청 시 dataType이 "jsonp" 또는 "script"이고 유형이 GET인 경우에만

을 사용하여 강제로 수정합니다. 문자 세트(charset). 일반적으로 로컬 및 원격 콘텐츠 인코딩이 다를 때 사용됩니다.



케이스 코드:

코드 복사 코드는 다음과 같습니다.

$(function(){

$('#send').click(function(){

$.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 = '
' 댓글['사용자 이름']

':


'

';

})

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

}

});

})


그런데 $.each() 함수는

$.each() 함수는 JQuery 객체의 Each() 메소드와는 달리 JQuery 객체를 연산하지 않는 전역 함수입니다. 대신 배열이나 객체를 첫 번째 매개변수로 취하고 두 번째 매개변수로 콜백 함수를 사용합니다. 콜백 함수에는 두 개의 매개변수가 있습니다. 첫 번째 매개변수는 객체의 멤버 또는 배열의 인덱스이고 두 번째 매개변수는 해당 변수 또는 콘텐츠입니다.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template