1
我正在创建一个表单,即在按钮上调用一个JavaScript文件以将表单的内容提交给一个php文件。我的问题是,这对IE浏览器有效,并且不适用于歌剧,谷歌浏览器或Firefox。在分析谷歌浏览器的控制台,我得到这个错误:(注:我已经缩短了本地主机的路径和删除我的IP地址)无法使用XMLHttpRequest执行php文件
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
有没有简单的方法来解决这一问题?我完全迷失在这里。
编辑:
我发现在我的代码中的错误,我打电话本地主机时,我应该已经使用相对路径(废话)。现在,这是固定的,我收到另一个错误。
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")
}
}
从错误,你似乎正在尝试做一个跨域请求(这是浏览器不允许)。然而,这只是一个疯狂的猜测,因为你没有提供你正在尝试制作的实际请求 – milo5b
如果你正在发出请求的页面和PHP脚本位于同一个域中,那么请使用相同的域来创建请求(或首先使用相对URL)。如果没有,那么看看CORS。 – CBroe