1

我正在创建一个表单,它在一个按钮上调用一个 javascript 文件以将表单的内容提交到一个 php 文件。我的问题是这适用于 IE,不适用于歌剧、谷歌浏览器或火狐。在 google chrome 中分析控制台时,我收到此错误:(注意:我缩短了 localhost 路径并删除了我的 ip 地址)

XMLHttpRequest cannot load http://localhost/browse-to-file.php.  No 'Access-Control-Allow-Origin' header is present on requested resource. Origin 'http://internal-ip-address' is therefore not allowed

另外,我还输出了 xmlHttpRequest() 状态码,如下所示:

Ready State: 4
Status: 0

我正在测试状态应该是 200。

有什么简单的方法可以解决这个问题吗?我完全迷失在这里。

编辑:

我在我的代码中发现了一个错误,当我应该使用相对路径(duh)时,我正在调用 localhost。既然已经解决了,我又遇到了另一个错误。

Uncaught TypeError: Cannot set property 'innerHTML' of null
xmlHttp.onreadystatechange

我的代码如下:

xmlHttp = new XMLHttpRequest();

然后是错误的部分:

  xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
        document.getElementById("result").innerHTML=xmlHttp.responseText;          
    } else{
        console.error("Ready State: " + xmlHttp.readyState)
        console.error("Status: " + xmlHttp.status)
        console.error("Status Text: " + xmlHttp.statusText);

        alert("An error has occured making the request")

    }
}
4

1 回答 1

0

答案真的很简单。首先,我必须将直接页面从“localhost”更改为服务器的实际 IP 地址。然后我意识到我正在尝试获取“结果”的元素 ID,这不是正确的 ID。但奇怪的是,这实际上确实适用于 IE10。

xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
    document.getElementById("result").innerHTML=xmlHttp.responseText;          
} else{
    console.error("Ready State: " + xmlHttp.readyState)
    console.error("Status: " + xmlHttp.status)
    console.error("Status Text: " + xmlHttp.statusText);

    alert("An error has occured making the request")

}
}

将上面的“结果”更改为正确的 id 后,它就可以工作了。

于 2014-03-02T19:32:52.493 回答