简体   繁体   English

Javascript AJAX功能在IE中不起作用?

[英]Javascript AJAX function not working in IE?

I have this code: 我有这个代码:

function render_message(id)
{
var xmlHttp;
  xmlHttp=new XMLHttpRequest();  
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
        document.getElementById('message').innerHTML=xmlHttp.responseText;
        document.getElementById('message').style.display='';
        }
    }
    var url="include/javascript/message.php";
    url=url+"?q="+id;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

For some reason it does not work in IE and an error is being reported on this line "document.getElementById('message').innerHTML=xmlHttp.responseText;" 由于某种原因,它在IE中不起作用,并且在此行上报告了错误“document.getElementById('message')。innerHTML = xmlHttp.responseText;” with an "Unknown Runtime Error". 带有“未知运行时错误”。

Can anyone help? 有人可以帮忙吗?

Edit: The code being added to the div is valid code ect. 编辑:添加到div的代码是有效代码等。

Here is the response: 以下是回复:

<div style="margin-left:auto;margin-right:auto;width:400px;">
    <img src="/forum/img/avatars/2.gif" width="90" height="89" style="float:left;">
    <div style="margin-left:100px;">
        <span style="font-size:16pt;">Sam152</a></span><br>
        <span style="font-size:10pt;text-transform:uppercase;font-weight:bold;">From Sam152</span><br>
        <span style="font-size:10pt;font-weight:bold;">Recieved April 17, 2009, 9:44 am</span><br>
        <br><br>

    </div>
</div>
<div style="margin-left:auto;margin-right:auto;width:400px;">
        asd</div>
<div style="margin-left:auto;margin-right:auto;width:400px;text-align:right;padding-top:10px;">
        <span onClick="requestPage('http://www.gametard.com/include/scripts/delete_message.php?id=14');document.getElementById('message14').style.display='none';document.getElementById('message').style.display='none';" class="button">Delete</span>
        <span onClick="document.getElementById('message').style.display='none';" class="button">Close</span>
        <span onClick="document.getElementById('to').value ='Sam152';document.getElementById('to').style.color ='#000';document.getElementById('newmessage').style.display='';" class="button">Reply</span>     

</div>

Not sure if the following applies to you as you don't mention what version of ie you are using. 不确定以下内容是否适用于您,因为您没有提到您正在使用的版本。

works only in ie7 upwards 仅适用于ie7以上

var xmlhttp=new XMLHttpRequest();

In Internet Explorer 5 and 6 you must use 在Internet Explorer 5和6中,您必须使用

var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

Your issue. 你的问题。

Essentially, you are trying to add HTML that doesn't make sense in the context you're adding it. 从本质上讲,您正在尝试添加在您添加它的上下文中没有意义的HTML。 Adding li's where there is no ul, or rows where there is no table can cause this sort of thing. 在没有ul的情况下添加li,或者在没有表的情况下添加行会导致这种情况。

Trust me on this: use an Ajax framework. 相信我:使用Ajax框架。

jQuery or prototype or any of the others. jQuery或原型或任何其他。

Don't roll your own - you're only seeing the tip of the cross-browser iceberg with this issue. 不要自己动手 - 你只是看到了这个问题的跨浏览器冰山一角。

If you must do it yourself check xmlHttp.status before getting your message. 如果你必须自己检查xmlHttp.status然后再收到你的消息。 Bear in mind that IE sometimes returns Windows error codes instead of HTTP status here, and older FX throws an exception if the error is a lost connection rather than a HTTP status. 请记住,IE有时会返回Windows错误代码而不是HTTP状态,如果错误是连接丢失而不是HTTP状态,则较旧的FX会抛出异常。

Oh, and IE6 is still about 30% of the web market and 60% of the corporate one, so @Paul Whelan's advice is worth checking too. 哦,IE6仍然是网络市场的30%左右,企业网络的60%,所以@Paul Whelan的建议也值得一试。

You can get this error if the result would be invalid HTML - for example <a><a>foo</a></a> or <a><p>foo</p></a> . 如果结果为无效HTML,您可能会收到此错误 - 例如<a><a>foo</a></a><a><p>foo</p></a>

Without seeing more of the code (what type of element "message" is and what responseText is) it's hard to say more. 没有看到更多的代码(什么类型的元素“消息”是什么和responseText是什么),很难说更多。

Your code isn't valid: 您的代码无效:

<span style="font-size:16pt;">Sam152</a></span>
                                    ^^^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM