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