我正在创建一个表单,在一个按钮上,调用一个javascript文件将表单的内容提交到php文件。我的问题是这适用于IE,并且不适用于opera,google chrome或firefox。在谷歌浏览器中分析控制台时,我收到此错误:(注意:我缩短了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.
有没有简单的方法来解决这个问题?我完全迷失在这里。
编辑:
我在我的代码中发现了一个错误,我在调用localhost时,我应该使用相对路径(duh)。现在已经修复了,我又收到了另一个错误。
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")
}
}
答案 0 :(得分: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后,就可以了。