2012-09-13 83 views
0

我有运行PHP脚本的AJAX请求,我想处理的情况是请求返回状态500IE9 AJAX错误:“无法完成操作由于错误c00c023f”

我的代码是工作在Chrome和Firfox,但在IE9上,如果我添加一个条件if(httpRequest.status==500)我得到这个JavaScript错误:

Could not complete the operation due to error c00c023f

我在页面上有一个加载图像,并且由于此错误,我无法替换图像,并且页面似乎仍在加载。正如这里引述:

Especially if you're displaying a "loading animation" or anything similar while the request is being executed. The result in my case was that it seemed as the page never stopped loading

首先,这里是我的AJAX调用代码:

function sendRequest(url, params, divToChange) 
{ 

    // code for IE7+, Firefox, Chrome, Opera, Safari 
    if (window.XMLHttpRequest) 
    { 
     var httpRequest = new XMLHttpRequest(); 
    } 
    else // code for IE6, IE5 
    { 
     var httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    //open the request and prepare variables for the PHP method being activated on server side// 
    httpRequest.open("POST", url, true); 

    //Set request headers for POST command// 
    httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    if(divToChange != "false") 
    { 
     httpRequest.onreadystatechange=function() 
     { 
      if(httpRequest.readyState==4 && httpRequest.status==200) 
      { 
       document.getElementById(divToChange).innerHTML=httpRequest.responseText; 
       eval(document.getElementById("runscript").innerHTML); 
      } 
      else if (httpRequest.status==500) 
      { 
       document.getElementById(divToChange).innerHTML=httpRequest.responseText; 

       //eval the lang variable from the php script. 
       eval(document.getElementById("runscript").innerHTML); 

       if(lang == "en") 
       { 
        document.getElementById(divToChange).innerHTML='<b> An error accured, please try again later</b>'; 
       } 
       else 
       { 
        document.getElementById(divToChange).innerHTML='<b> שגיאה התרחשה, בבקשה נסה שנית מאוחר יותר</b>'; 
       }  
      } 
     } 
    } 

    httpRequest.send(params); //sending the request to the server// 
} 

我搜索一下,发现this post on stack。从那里,我发现this quote

Just before you call the XmlHttpRequest.abort() method, set a flag on it, this is how I did it:

XmlHttpRequest.aborted = true; 
XmlHttpRequest.abort(); 
In the onreadystatechanged handler, the first lines of code in my case is: 
if (xmlHttpRequest.aborted==true) { 
    stopLoadAnimation(); 
    return; 
} 

但在我而言,我不甚至称XmlHttpRequest.abort(),所以为什么我获得第一名的错误?顺便说一句,如果我删除所有的条件else if (httpRequest.status==500)和它的所有代码,我不会得到错误,但是,然后再次,我不能够显示消息500错误。

回答

0

当你对innerHTML很感兴趣并且创建它无法理解的无效标记时,IE会做一些非常奇怪的事情。 (例如,我遇到了一个非常类似的问题,例如试图在表单中嵌入表单的人)。

我打赌divToChange没有引用有效的HTML div元素,并且您的错误来自尝试改变div本身。

在你调试代码,添加console.log(divToChange)(或者你可以使用alert(divToChange),应该这样说:"[object HTMLDivElement]或类似的东西。

在您的测试,如果你想确保divToChangefalsey [1 ],简单地做

if (divToChange) 

[1] JavaScript的的falseytruthy定义可能需要一些如果你来自一个强类型的环境习惯。

+0

以下的答案,我已经审查了我的代码,并确实发现我的Ajax函数在创建状态消息的容器div之前被调用。我不知道它是如何工作在Chrome/Firefox上,有时在IE上,但无论如何,我已经修复它,现在看起来没问题。感谢:) –

1

我在猜测,但我认为缺乏readyState检查可能会导致您一些问题。请记住,在调用完成时,不要只调用onReadyStateChange处理程序 - 它会在每次状态更改时调用它。因此,前两个(Unitialized和Loadinf)我不相信“状态”属性是可用的 - IE可能会抛出一个奇怪的错误,因为ActiveX和JavaScript之间的交互。

我建议像这样为你的错误状态(注意,有超过500个其他许多错误,你可能想赶上以及):

(httpRequest.readyState> 1 & & httpRequest.status!= 200)

我只能猜测其他浏览器在一开始就创建状态属性(如果这确实是问题)。

另外我也可能会建议(虚心地)你给我的AJAX抽象库一个尝试。它会自动处理几乎所有这些。既可以设置成功又可以设置错误处理程序,不用担心跨浏览器的问题,并且它们都可以无缝地融入到您现有的代码库中。

组件是在这里:

http://depressedpress.com/javascript-extensions/dp_ajax/

我发现它有用的疯狂。

+0

感谢您的答案,我会确实看看你的AJAX库。 –

0

我试过这个,它对我有用。希望这有助于:

if (xmlhttp.readyState==1) { return }  // added this to fix error c00c023f in IE9 
       else if (xmlhttp.readyState==4) 
        { 
         if(xmlhttp.status==200) { func(xmlhttp.responseText) } 
        } 
相关问题