jQuery Ajax函数

来源:互联网 发布:重庆大学网络教育仙游 编辑:程序博客网 时间:2024/06/06 14:19
 函数:$.ajax(properties)

功能:用一个http请求访问一个远程页面
返回:XMLHttpRequest

参数;
(String)url请求地址,
(String)type请求类型("GET","POST")
(String)dataType:从服务器端返回的数据类型("xml","html","script","json")
(Boolean)ifModified:根据Last-Modified header判断返回内容是否改变
(Number)timeout:请求超时
(Boolean)global:对此次请求是否引起全局的ajax事件出来,默认true
(Function)error:错误处理函数
(Function)complete:请求完成后的处理函数
(Object|String)data:发送到服务器端的数据
(String) contentType :默认"application/x-www-form-urlencoded"
(Boolean) processData :传输到服务器端的数据默认被转换到query string中以适合默认"application/x-www-form-urlencoded"

方式,如果你想以DOMDocuments方式传输数据,就将该选项设为false
(Boolean)aysnc:是否异步传输,默认为true
(Function)beforeSend:请求前响应函数

例子:
Load and execute a JavaScript file.
[CODE=javascript]$.ajax({
type: "GET",
url: "test.js",
dataType: "script"
});[/CODE]
Save some data to the server and notify the user once its complete.
[CODE=javascript]$.ajax({
type: "POST",
url: "some.php",
data: "name=John&location=Boston",
success: function(msg){
alert( "Data Saved: " + msg );
}
});[/CODE]
jQuery Code
[CODE=javascript]var html = $.ajax({
url: "some.php",
async: false
}).responseText;[/CODE]
jQuery Code
[CODE=javascript]var xmlDocument = [create xml document];
$.ajax({
url: "page.php",
processData: false,
data: xmlDocument,
success: handleResponse
});[/CODE]
函数:$.ajaxSetup(settings),$.ajaxTimeout(time)
功能:设定请求的一些参数
返回:undefined

例子:
[CODE=javascript]$.ajaxSetup( {
url: "/xmlhttp/",
global: false,
type: "POST"
});
$.ajaxTimeout( 5000 );
[/CODE]
函数:$.get(url, params, callback),$.getIfModified(url, params, callback),
$.getJSON(url, params, callback),$.getScript(url, callback)

功能:get方式提交数据

例子:
[CODE=javascript]$.get("test.cgi",
{ name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
}
);[/CODE]
函数:$.post(url, params, callback)
功能:post方式提交数据

例子:
[CODE=javascript]$.post("test.cgi",
{ name: "John", time: "2pm" },
function(data){
alert("Data Loaded: " + data);
}
);[/CODE]
函数:ajaxComplete(callback),ajaxComplete(callback),ajaxSend(callback)
ajaxStart(callback),ajaxStop(callback),ajaxSuccess(callback)

功能:XMLHttpRequest状态改变过程中各个响应处理函数

例子:
[CODE=javascript]$("#msg").ajaxComplete(function(request, settings){
$(this).append("
  • Request Complete.
  • ");
    });

    $("#msg").ajaxSuccess(function(request, settings){
    $(this).append("
  • Successful Request!
  • ");
    });

    $("#loading").ajaxStop(function(){
    $(this).hide();
    });

    $("#msg").ajaxSend(function(request, settings){
    $(this).append("
  • Starting request at " + settings.url + "
  • ");
    });[/CODE]
    函数:load(url, params, callback),loadIfModified(url, params, callback)

    功能:加载html内容
    返回:jQuery对象
    参数:同get和post方式提交

    例子:
    $("#feeds").load("feeds.html");
    Before
    Result:
    45 feeds found.

    [CODE=javascript]$("#feeds").load("feeds.html",
    {limit: 25},
    function() { alert("The last 25 entries in the feed have been loaded"); }
    );[/CODE]
    函数:serialize()

    功能:将表单元素和值序列化成string
    返回:String

    例子:
    $("input[@type=text]").serialize();
    Before

    Result
    name=John&location=Boston
    原创粉丝点击