jquery除了提供get和post请求交互外,提供了一个极其强大和灵活的调用方式$.ajax(),通过设置此函数的具体参数对象信息,可以实现各种的调用方式和返回信息。
Load a remote page using an HTTP request.
This is jQuery's low-level AJAX implementation. See $.get, $.post etc. for higher-level abstractions that are often easier to understand and use, but
don't offer as much functionality (such as error callbacks).
$.ajax() returns the XMLHttpRequest that it creates. In most cases you won't need that object to manipulate directly, but it is available if you need to
abort the request manually.
Note: If you specify the dataType option described below, make sure the server sends the correct MIME type in the response (eg. xml as "text/xml").
Sending the wrong MIME type can lead to unexpected problems in your script. See Specifying the Data Type for AJAX Requests for more information.
$.ajax() takes one argument, an object of key/value pairs, that are used to initialize and handle the request. See below for a full list of the key/values
that can be used.
As of jQuery 1.2, you can load JSON data located on another domain if you specify a JSONP callback, which can be done like so: "myurl?callback=?". jQuery
automatically replaces the ? with the correct method name to call, calling your specified callback. Or, if you set the dataType to "jsonp" a callback will
be automatically added to your Ajax request.
$.ajax({
url:"test",
type:"Post",
success:back,
dataType:"xml",
data: "name="+userName
}) ;
调用成功后jquery会自动调用回调函数,我们可以获取服务器端返回的xml数据,为了处理xml数据,要先将数据转换成jquery的对象,然后通过jquery提供的筛选函数
及jquery对象属性获取所需的文本内容。
find(expr)
Searches for all elements that match the specified expression. This method is a good way to find additional descendant elements with which to process.
All searching is done using a jQuery expression. The expression can be written using CSS 1-3 Selector syntax.
text()
Get the text contents of all matched elements.
The result is a string that contains the combined text contents of all matched elements. This method works on both HTML and XML documents.
回调函数
function back(result){
var messageNode=$(result).find("message");
var messageString=messageNode.text();
var resultNode=$("#result");
resultNode.html(messageString);
}
AJAX详细参数设置
async (Boolean) : (默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必
须等待请求完成才可以执行。
beforeSend (Function) : 发送请求前可修改 XMLHttpRequest 对象的函数,如添加自定义 HTTP 头。XMLHttpRequest 对象是唯一的参数。这是一个 Ajax 事件。如果返回
false可以取消本次ajax请求。
function (XMLHttpRequest) {
this; // 调用本次AJAX请求时传递的options参数
}
cache (Boolean) : (默认: true,dataType为script时默认为false) jQuery 1.2 新功能,设置为 false 将不会从浏览器缓存中加载请求信息。
complete (Function) : 请求完成后回调函数 (请求成功或失败时均调用)。参数: XMLHttpRequest 对象和一个描述成功请求类型的字符串。 Ajax 事件。
function (XMLHttpRequest, textStatus) {
this; // 调用本次AJAX请求时传递的options参数
}
contentType (String) : (默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数应用场合。
data (Object,String) : 发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为
Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'。
dataFilter (Function) :给Ajax返回的原始数据的进行预处理的函数。提供data和type两个参数:data是Ajax返回的原始数据,type是调用jQuery.ajax时提供的dataType参数
。函数返回的值将由jQuery进一步处理。
function (data, type) {
// 对Ajax返回的原始数据进行预处理
return data // 返回处理后的数据
}
dataType (String) : (默认值:智能判断xml或者html)预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText
,并作为回调函数参数传递,可用值:
"xml": 返回 XML 文档,可用 jQuery 处理。
"html": 返回纯文本 HTML 信息;包含的script标签会在插入dom时执行。
"script": 返回纯文本 JavaScript 代码。不会自动缓存结果。除非设置了"cache"参数。注意:在远程请求时(不在同一个域下),所有POST请求都将转为GET请求。(因为将使
用DOM的script标签来加载)
"json": 返回 JSON 数据。
"jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。
"text": 返回纯文本字符串
error (Function) : (默认: 自动判断 (xml 或 html)) 请求失败时调用时间。参数有以下三个:XMLHttpRequest 对象、错误信息、(可选)捕获的错误对象。如果发生了错
误,错误信息(第二个参数)除了得到null之外,还可能是"timeout", "error", "notmodified" 和 "parsererror"。Ajax 事件。
function (XMLHttpRequest, textStatus, errorThrown) {
// 通常 textStatus 和 errorThrown 之中
// 只有一个会包含信息
this; // 调用本次AJAX请求时传递的options参数
}
global (Boolean) : (默认: true) 是否触发全局 AJAX 事件。设置为 false 将不会触发全局 AJAX 事件,如 ajaxStart 或 ajaxStop 可用于控制不同的 Ajax 事件。
ifModified (Boolean) : (默认: false) 仅在服务器数据改变时获取新数据。使用 HTTP 包 Last-Modified 头信息判断。
jsonp (String) : 在一个jsonp请求中重写回调函数的名字。这个值用来替代在"callback=?"这种GET或POST请求中URL参数里的"callback"部分,比如{jsonp:'onJsonPLoad'}
会导致将"onJsonPLoad=?"传给服务器。
password (String) : 用于响应HTTP访问认证请求的密码
processData (Boolean) : (默认: true) 默认情况下,发送的数据将被转换为对象(技术上讲并非字符串) 以配合默认内容类型 "application/x-www-form-urlencoded"。如果
要发送 DOM 树信息或其它不希望转换的信息,请设置为 false。
scriptCharset (String) : 只有当请求时dataType为"jsonp"或"script",并且type是"GET"才会用于强制修改charset。通常在本地和远程的内容编码不同时使用。
success (Function) : 请求成功后的回调函数。参数:由服务器返回,并根据dataType参数进行处理后的数据;描述状态的字符串。 Ajax 事件。
function (data, textStatus) {
// data 可能是 xmlDoc, jsonObj, html, text, 等等...
this; // 调用本次AJAX请求时传递的options参数
}
timeout (Number) : 设置请求超时时间(毫秒)。此设置将覆盖全局设置。
type (String) : (默认: "GET") 请求方式 ("POST" 或 "GET"), 默认为 "GET"。注意:其它 HTTP 请求方法,如 PUT 和 DELETE 也可以使用,但仅部分浏览器支持。
url (String) : (默认: 当前页地址) 发送请求的地址。
username (String) : 用于响应HTTP访问认证请求的用户名
xhr (Function) : 需要返回一个XMLHttpRequest 对象。默认在IE下是ActiveXObject 而其他情况下是XMLHttpRequest 。用于重写或者提供一个增强的XMLHttpRequest 对象。
这个参数在jQuery 1.3以前不可用。
示例
加载并执行一个 JS 文件。
jQuery 代码:
$.ajax({
type: "GET",
url: "test.js",
dataType: "script"
});
--------------------------------------------------------------------------------
保存数据到服务器,成功时显示信息。
jQuery 代码:
$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});
--------------------------------------------------------------------------------
装入一个 HTML 网页最新版本。
jQuery 代码:
$.ajax({
url: "test.html",
cache: false,
success: function(html){
$("#results").append(html);
}
});
--------------------------------------------------------------------------------
同步加载数据。发送请求时锁住浏览器。需要锁定用户交互操作时使用同步方式。
jQuery 代码:
var html = $.ajax({
url: "some.php",
async: false
}).responseText;
--------------------------------------------------------------------------------
发送 XML 数据至服务器。设置 processData 选项为 false,防止自动转换数据格式。
jQuery 代码:
var xmlDocument = [create xml document];
$.ajax({
url: "page.php",
processData: false,
data: xmlDocument,
success: handleResponse
});
分享到:
相关推荐
ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习笔记源代码ajax 学习...
AJAX学习笔记。内含Java调用的两个实例,及JavaScript的ajax工具函数,可快速入门,也可当手册使用。个人精心整理,值得收藏。 更多:http://download.csdn.net/user/daillo/all
Ajax 学习笔记,超详细的噢!不看后悔,自己上培训班时记的笔记
ajax学习笔记。。。。。。。。。。。。。。。。。。。。。。
根据个人学习Ajax的过程总结,整理了Ajax相关基础知识点,对Ajax的使用有较详细的介绍,通过个别举例,可以快速了解Ajax的功能,比较适合初学者入门。
Ajax学习笔记.zip
夏玉保整理Ajax学习笔记.doc 夏玉保整理Ajax学习笔记.doc夏玉保整理Ajax学习笔记.doc 夏玉保整理Ajax学习笔记.doc
Ajax学习笔记
web前端开发AJAX学习笔记
ZK-AJAX学习笔记
jQuery+AJAX学习笔记
Myeclipse的源代码示例(ajax学习笔记代码),四天学会ajax doc文档。
16ajax学习笔记1
Web2.0之Ajax 学习笔记与应用实例,Web2.0之Ajax 学习笔记与应用实例
非常经典的ajax学习笔记,想学好ajax的程序员都应该学习一下
Ajax 学习笔记(1).rtf
ASP.NET Ajax学习笔记 第一章 ASP.NET AJAX基础 介绍ASP.NET AJAX 简单接处ASP.NET AJAX
AJAX学习笔记忘记了看一眼
ajax的学习笔记,由浅入深。ajax初学的朋友值得借鉴和参考。
Unix 学习笔记 Core Java 学习笔记 Tiger 学习笔记 XML 学习笔记 Oracle 学习笔记 JDBC 学习笔记 Hibernate 学习笔记 HTML&JavaScript 学习笔记 Servlet 学习笔记 ...Ajax 学习笔记 EJB 学习笔记 Spring 学习笔记