ct parser.php 错误,jQuery ajax 在 IE 中碰到 parsererror 异常的分析与应对

在用 jquery 1.4.2 的 $.ajax() 时:

$.ajax({

url: "updateRoles.action",

data: form.serialize(),

dataType: "json",

success: function(msg){

},

complete: function(xhr,msg){

alert(msg);

}

});

1

2

3

4

5

6

7

8

9

10

11

$.ajax({

url:"updateRoles.action",

data:form.serialize(),

dataType:"json",

success:function(msg){

},

complete:function(xhr,msg){

alert(msg);

}

});

在其他浏览器中表现的很好,但是在 IE(7/8) 下总报 parsererror 错误。在 jquery 中找到 parsererror,定位在 5989 行:

if ( status === "success" ) {

// Watch for, and catch, XML document parse errors

try {

// process the data (runs the xml through httpData regardless of callback)

data = jQuery.httpData( xhr, s.dataType, s );

} catch( parserError ) {

status = "parsererror";

errMsg = parserError;

}

}

1

2

3

4

5

6

7

8

9

10

if(status==="success"){

// Watch for, and catch, XML document parse errors

try{

// process the data (runs the xml through httpData regardless of callback)

data=jQuery.httpData(xhr,s.dataType,s);

}catch(parserError){

status="parsererror";

errMsg=parserError;

}

}

那应该是在执行 data = jQuery.httpData( xhr, s.dataType, s ); 时报的异常,来看 httpData 函数的局部代码:

httpData: function( xhr, type, s ) {

var ct = xhr.getResponseHeader("content-type") || "",

xml = type === "xml" || !type && ct.indexOf("xml") >= 0,

data = xml ? xhr.responseXML : xhr.responseText;

if ( xml && data.documentElement.nodeName === "parsererror" ) {

jQuery.error( "parsererror" );

}

1

2

3

4

5

6

7

8

httpData:function(xhr,type,s){

varct=xhr.getResponseHeader("content-type")||"",

xml=type==="xml"||!type&&ct.indexOf("xml")>=0,

data=xml?xhr.responseXML:xhr.responseText;

if(xml&&data.documentElement.nodeName==="parsererror"){

jQuery.error("parsererror");

}

如果在 IE 中加入 alert() 来诊断的话,你会发现在第一行 var ct = xhr.getResponseHeader("content-type") || "" 处就抛出异常了。原因是在 IE 中的 XMLHttpRequest(jquery 1.4.4 在 IE 中使用 new window.XMLHttpRequest() 构造出 XHR 对象的) 是不存在 getResponseHeader() 方法的,所以不能调用它。而 FireFox 等其他浏览器是存在 getResponseHeader() 函数的。

在 IE 中的 XMLHttpRequest 对象拥有的属性和方法有:

status

timeout

ontimeout

responseXML

responseBody

onreadystatechange

readyState

responseText

statusText

XMLHttpRequest 对象在 FireFox 中拥有的属性和方法有:

abort

dispatchEvent

removeEventListener

open

setRequestHeader

onreadystatechange

send

readyState

status

channel

responseXML

responseText

statusText

getAllResponseHeaders

getResponseHeader

sendAsBinary

overrideMimeType

multipart

mozBackgroundRequest

withCredentials

upload

onuploadprogress

onabort

onerror

onload

onloadstart

onprogress

getInterface

所以一个临时的解决办法是修改 jquery.js 里的一行源代码,把 6229 行处的:

var ct = xhr.getResponseHeader("content-type") || "",

1

varct=xhr.getResponseHeader("content-type")||"",

改为:

var ct = xhr.getResponseHeader ? xhr.getResponseHeader("content-type") || "":"",

1

varct=xhr.getResponseHeader?xhr.getResponseHeader("content-type")||"":"",

先判断一下是否存在 getResponseHeader() 方法。问题暂时是能解决了,只是决得事情有些蹊跷,堂堂一个举世闻名的 jquery 组件不太可能犯下如此低级的错误,而且它以前在我的 IE 中也好像是正常的。

想知道更优美的解决方案,不能动到 jquery 的核心代码,或者出现这一情况的真正原因是什么呢?

或许可以在 complete 回调函数中判断到第二个参数 textStatus 是 parsererror 时按正常情况进行处理。

有人说后台 header('Content-type: text/html; charset=utf8'); 的 charset=utf8 应写成 charset=utf-8,可实际 FireFox 里看到的 charset 是 utf-8 也是不行的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值