我正在创建一个表单,它在一个按钮上调用一个 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")
}
}