What are the ajax functions?
The ajax functions include: 1. "$(selector).load()", used to load remote data into the selected element; 2. "$.ajax()"; 3. "$ .get()"; 4. "$.post()"; 5. "$.getJSON()"; 6. "$.getScript()".
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
AJAX request function
Request | Description | |
---|---|---|
$(selector).load(url,data,callback) | Load remote data into the selected element | |
$.ajax(options) | Load remote data into the XMLHttpRequest object | |
$.get(url,data,callback,type) | Use HTTP GET to load remote data | |
$.post(url,data,callback,type) | Use HTTP POST to load remote data | |
Use HTTP GET to load remote JSON data | ||
Load and execute Remote JavaScript file |
Parameter name | Type | Description |
url | String | (default: current page address) The address to send the request. |
type | String | (Default: “GET”) Request method (“POST” or “GET”), default is “GET” . Note: Other HTTP request methods such as PUT and DELETE can also be used, but are only supported by some browsers. |
timeout | Number | Set the request timeout (milliseconds). This setting overrides the global setting. |
async | Boolean | (Default: true) By default, all requests are asynchronous requests. If you need to send a synchronous request, please set this option to false. 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. |
beforeSend | Function | Functions that can modify the XMLHttpRequest object before sending the request, such as adding custom HTTP headers. The XMLHttpRequest object is the only parameter. function (XMLHttpRequest) { this; // the options for this ajax request } function (XMLHttpRequest) { this; // the options for this ajax request } |
cache | Boolean | (Default: true) jQuery 1.2 new feature, setting it to false will not Load request information in browser cache. |
complete | Function | Callback function after the request is completed (called when the request succeeds or fails). Parameters: XMLHttpRequest object, success information string. function (XMLHttpRequest, textStatus) { this; // the options for this ajax request } function (XMLHttpRequest, textStatus) { this; // the options for this ajax request } |
contentType | String | (Default: “application/x-www-form-urlencoded”) Send message to Server content encoding type. The default value is suitable for most applications. |
data | Object, String | Data sent to the server. Will be automatically converted to request string format. Appended to the URL in GET requests. See the processData option description to disable this automatic conversion. Must be in Key/Value format. 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′. |
dataType | String | The expected data type returned by the server. If not specified, jQuery will automatically return responseXML or responseText based on the HTTP packet MIME information and pass it as a callback function parameter. Available values: "xml": Return an XML document that can be processed by jQuery. "html": Returns plain text HTML information; contains script elements. "script": Returns plain text JavaScript code. Results are not cached automatically. "json": Returns JSON data. "jsonp": JSONP format. When calling a function using JSONP format, such as "myurl?callback=?" jQuery will automatically replace ? with the correct function name to execute the callback function. |
error | Function | (Default: automatic judgment (xml or html)) This method will be called when the request fails. This method has three parameters: XMLHttpRequest object, error message, and (possibly) captured error object. function (XMLHttpRequest, textStatus, errorThrown) { // Normally only one of textStatus and errorThown has a value this; // the options for this ajax request } function (XMLHttpRequest, textStatus, errorThrown) { // Normally only one of textStatus and errorThown has a value this; // the options for this ajax request } |
global | Boolean | (Default: true) Whether to trigger global AJAX events. Setting to false will not trigger global AJAX events, such as ajaxStart or ajaxStop. Can be used to control different Ajax events |
Boolean | (Default: false) Only get new data when the server data changes . | Use HTTP packet Last-Modified header information to determine. |
processData | Boolean | (默认: true) 默认情况下,发送的数据将被转换为对象(技术上讲并非字符串) 以配合默认内容类型 “application/x-www-form-urlencoded”。 如果要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。 |
success | Function | 请求成功后回调函数。这个方法有两个参数:服务器返回数据,返回状态 function (data, textStatus) { // data could be xmlDoc, jsonObj, html, text, etc... this; // the options for this ajax request } function (data, textStatus) { // data could be xmlDoc, jsonObj, html, text, etc... this; // the options for this ajax request } |
你可以指定xml、script、html、json作为其数据类型,可以为beforeSend、error、sucess、complete等状态设置 处理函数,众多其它参数也可以订完完全全定义用户的Ajax体验。下面的例子中,我们用ajax()来调用一个XML文档:
$.ajax({ url: 'doc.xml', type: 'GET', dataType: 'xml', timeout: 1000, error: function(){ alert('Error loading XML document'); }, success: function(xml){ alert(xml); //此处xml就是XML的jQuery对象了,你可以用find()、next()或XPath等方法在里面寻找节点, 和用jQuery操作HTML对象没有区别 } });
进一步了解AJAX事件
前面讨论的一些方法都有自己的事件处理机制,从页面整体来说,都只能说是局部函数。jQuery提供了AJAX全局函数的定义,以满足特殊的需求。下面是jQuery提供的所有函数(按照触发顺序排列如下):
ajaxStart
(全局事件) 开始新的Ajax请求,并且此时没有其他ajax请求正在进行beforeSend
(局部事件) 当一个Ajax请求开始时触发。如果需要,你可以在这里设置XMLHttpRequest对象ajaxSend
(全局事件) 请求开始前触发的全局事件success
(局部事件) 请求成功时触发。即服务器没有返回错误,返回的数据也没有错误ajaxSuccess
全局事件全局的请求成功error
(局部事件) 仅当发生错误时触发。你无法同时执行success和error两个回调函数ajaxError
全局事件全局的发生错误时触发complete
(局部事件) 不管你请求成功还是失败,即便是同步请求,你都能在请求完成时触发这个事件ajaxComplete
全局事件全局的请求完成时触发ajaxStop
(全局事件) 当没有Ajax正在进行中的时候,触发
局部事件在之前的函数中都有介绍,我们主要来看看全局事件。对某个对象进行全局事件监听,那么全局中的AJAX动作,都会对其产生影响。比如,当页面在进行AJAX操作时,ID为”loading”的p就显示出来:
$("#loading").ajaxStart(function(){ $(this).show(); });
全局事件也可以帮助你编写全局的错误相应和成功相应,而不需要为每个AJAX请求独立设置。有必要指出,全局事件的参数是很有用的。除了 ajaxStart、ajaxOptions,其他事件均有event, XMLHttpRequest, ajaxOptions三个参数。第一个参数即事件本身;第二个是XHR对象;第三个是你传递的ajax参数对象。在一个对象里显示全局的AJAX情况:
$("#msg").beforeSend(function(e,xhr,o) { $(this).html("正在请求"+o.url); }).ajaxSuccess(function(e,xhr,o) { $(this).html(o.url+"请求成功"); }).ajaxError(function(e,xhr,o) { $(this).html(o.url+"请求失败"); });
很显然,第三个参数也可以帮助你传递你在AJAX事件里加入的自定义参数。 在单个AJAX请求时,你可以将global的值设为false,以将此请求独立于AJAX的全局事件。
$.ajax({ url: "request.php", global: false, // 禁用全局Ajax事件. });
如果你想为全局AJAX设置参数,你会用上ajaxSetup()函数。例如,将所有AJAX请求都传递到request.php,;禁用全局方法;强制用POST方法传递:
$.ajaxSetup({ url: "request.php", global: false, type: "POST" });
【相关教程推荐:AJAX视频教程】
The above is the detailed content of What are the ajax functions?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.
