zoukankan      html  css  js  c++  java
  • Jquery中AJAX参数详细介绍

    Jquery中AJAX参数详细列表:

    参数名 类型 描述
    url String (默认: 当前页地址) 发送请求的地址。
    type String (默认: "GET") 请求方式 ("POST" 或 "GET"), 默认为 "GET"。注意:其它 HTTP 请求方法,如 PUT 和 DELETE 也可以使用,但仅部分浏览器支持。
    timeout Number 设置请求超时时间(毫秒)。此设置将覆盖全局设置。
    async Boolean (默认: true) 默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。
    beforeSend Function 发送请求前可修改 XMLHttpRequest 对象的函数,如添加自定义 HTTP 头。XMLHttpRequest 对象是唯一的参数。
    function (XMLHttpRequest) {
    this; // the options for this ajax request
    }
    cache Boolean (默认: true) jQuery 1.2 新功能,设置为 false 将不会从浏览器缓存中加载请求信息。
    complete Function 请求完成后回调函数 (请求成功或失败时均调用)。参数: XMLHttpRequest 对象,成功信息字符串。
    function (XMLHttpRequest, textStatus) {
    this; // the options for this ajax request
    }
    contentType String (默认: "application/x-www-form-urlencoded") 发送信息至服务器时内容编码类型。默认值适合大多数应用场合。
    data Object,
    String
    发 送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'。
    dataType String

    预期服务器返回的数据类型。如果不指定,jQuery 将自动根据 HTTP 包 MIME 信息返回 responseXML 或 responseText,并作为回调函数参数传递,可用值:

    "xml": 返回 XML 文档,可用 jQuery 处理。

    "html": 返回纯文本 HTML 信息;包含 script 元素。

    "script": 返回纯文本 JavaScript 代码。不会自动缓存结果。

    "json": 返回 JSON 数据 。

    "jsonp": JSONP 格式。使用 JSONP 形式调用函数时,如 "myurl?callback=?" jQuery 将自动替换 ? 为正确的函数名,以执行回调函数。

    error Function (默认: 自动判断 (xml 或 html)) 请求失败时将调用此方法。这个方法有三个参数:XMLHttpRequest 对象,错误信息,(可能)捕获的错误对象。
    function (XMLHttpRequest, textStatus, errorThrown) {
    // 通常情况下textStatus和errorThown只有其中一个有值
    this; // the options for this ajax request
    }
    global Boolean (默认: true) 是否触发全局 AJAX 事件。设置为 false 将不会触发全局 AJAX 事件,如 ajaxStart 或 ajaxStop 。可用于控制不同的Ajax事件
    ifModified Boolean (默认: false) 仅在服务器数据改变时获取新数据。使用 HTTP 包 Last-Modified 头信息判断。
    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
    }
          $(document).ready(function() {
                jQuery("#clearCac").click(function() {
                    jQuery.ajax({
                        url: "/Handle/Do.aspx",
                        type: "post",
                        data: { id: '0' },
                        dataType: "json",
                        success: function(msg) {
                            alert(msg);
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest.status);
                            alert(XMLHttpRequest.readyState);
                            alert(textStatus);
                        },
                        complete: function(XMLHttpRequest, textStatus) {
                            this; // 调用本次AJAX请求时传递的options参数
                        }
                    });
                });
            });

    一、error:function (XMLHttpRequest, textStatus, errorThrown)
    (默认: 自动判断 (xml 或 html)) 请求失败时调用时间。参数有以下三个:XMLHttpRequest 对象、错误信息、(可选)捕获的错误对象。如果发生了错误,错误信息(第二个参数)除了得到null之外,还可能是"timeout", "error", "notmodified" 和 "parsererror"。
    textStatus: "timeout", "error", "notmodified" 和 "parsererror"。
    二、error事件返回的第一个参数XMLHttpRequest有一些有用的信息:
    XMLHttpRequest.readyState:
    状态码:
    0 - (未初始化)还没有调用send()方法
    1 - (载入)已调用send()方法,正在发送请求
    2 - (载入完成)send()方法执行完成,已经接收到全部响应内容
    3 - (交互)正在解析响应内容
    4 - (完成)响应内容解析完成,可以在客户端调用了
    三、data:"{}", data为空也一定要传"{}";不然返回的是xml格式的。并提示parsererror.
    四、parsererror的异常和Header 类型也有关系。及编码header('Content-type: text/html; charset=utf8');
    五、XMLHttpRequest.status:略

  • 相关阅读:
    【转】在xcode5中修改整个项目名
    Mac: Alias[设置命令的别名]
    合并静态库文件
    Xcode6:模拟器消失了?
    Xcode6: CocoaPods 错误 target overrides the `OTHER_LDFLAGS`...
    element-ui之layout布局el-row标签
    sqlserver 数据库显示"正在恢复"的解决办法
    sql server 收缩日志
    mysql Client does not support authtication protocol requested by server;consider upgrading mysql client
    mysql安装版安装教程
  • 原文地址:https://www.cnblogs.com/qiufuwu618/p/2826190.html
Copyright © 2011-2022 走看看