jquery使我们在开发ajax应用程序的时候提高了效率,减少了许多兼容性问题,我们在ajax项目中,遇到ajax异步获取数据出错怎么办,我们可以通过捕捉error事件来获取出错的信息。
在没给大家介绍正文之前先给分享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
}
jquery中ajax的常用用法类似于:
jscript 代码
$(document).ready(function() { jquery(#clearcac).click(function() { jquery.ajax({ url: url, 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参数 } }); }); });
当通过ajax异步调用成功时,会调用 success函数 。success函数语法为:
//请求成功后回调函数。这个方法有两个参数:服务器返回数据,返回状态 function (data, textstatus) { // data could be xmldoc, jsonobj, html, text, etc... this; // the options for this ajax request }当通过ajax异步调用出错时,会调用 error函数 。error函数语法为://(默 认: 自动判断 (xml 或 html)) 请求失败时调用时间。参数有以下三个:xmlhttprequest 对象、错误信息、(可选)捕获的错误对象。如果发生了错误,错误信息(第二个参数)除了得到null之外,还可能是timeout, error, notmodified 和 parsererror。//textstatus: timeout, error, notmodified 和 parsererror。error:function (xmlhttprequest, textstatus, errorthrown) { }
error事件返回的第一个参数xmlhttprequest:
xmlhttprequest.readystate: 状态码的意思
0 - (未初始化)还没有调用send()方法
1 - (载入)已调用send()方法,正在发送请求
2 - (载入完成)send()方法执行完成,已经接收到全部响应内容
3 - (交互)正在解析响应内容
4 - (完成)响应内容解析完成,可以在客户端调用了
发送error可能有下面两张引起的,或者其他程序问题,需要我们认真仔细。
1、data:{}, data为空也一定要传{};不然返回的是xml格式的。并提示parsererror.
2、parsererror的异常和header 类型也有关系。及编码header('content-type: text/html; charset=utf8');
以上内容是小编给大家分享关于jquery ajax error 调试错误的技巧,希望大家喜欢。