Javascript AJAX 函数在 IE 中不起作用?

标签 javascript internet-explorer xmlhttprequest

我有这个代码:

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);
}

出于某种原因,它在 IE 中不起作用,并且在“document.getElementById('message').innerHTML=xmlHttp.responseText;”这一行报告错误出现“未知的运行时错误”。

有人能帮忙吗?

编辑:添加到 div 的代码是有效代码等。

这是响应:

<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>

最佳答案

不确定以下内容是否适用于您,因为您没有提及您使用的是哪个版本。

仅适用于ie7以上

var xmlhttp=new XMLHttpRequest();

在 Internet Explorer 5 和 6 中,您必须使用

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

关于Javascript AJAX 函数在 IE 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/760628/

相关文章:

javascript - 为什么删除sessionStorage后仍然存在

html - Internet Explorer 中的隐藏文本

javascript - 选项 资源加载失败,仅在 Chrome 中

javascript - 使用 PHP move_uploaded_file() 时出错

javascript - 为什么通过XHR的异步onreadystatechange事件递归会消耗栈?

javascript - 来自 Vue 组件数据的计算函数

javascript - setImmediate() 函数在 setTimeout() 函数之后调用

Javascript 和 CSS 动画

css - Wordpress floatbox 问题 IE

php - Internet Explorer 无法从 PHP/jQuery 刷新