我有一個在nodejs中使用Express開發的http服務器。 服務器在端口3000本地運行。CORS仍然處於SecurityError狀態:操作不安全
有一個html頁面(index.html),它調用ajax GET內容(作爲html內容類型)。 這些ajax內容也在同一個端口上的相同服務器上提供,位於相同的http protocole中。
在節點服務器應用程序中,我添加了Cors Same Origin標頭,但是index.html在控制檯中仍然存在錯誤:「安全錯誤:操作不安全」。
在瀏覽器控制檯,我看到成功的頭約「訪問控制允許來源」節點快速應用,等等
此外,相同的應用也爲另一位頁面, index.html可以成功獲取沒有任何安全錯誤的數據。
您有任何其他建議嗎?
function getData(url, type, CType, id) {
//var xhr = new XMLHttpRequest();
//xhr.open(type, url, true);
//xhr.withCredentials = true;
//xhr.onload = function() {
//console.log(xhr.responseText);
//if(CType == 'text/html') { $(id).append(xhr.responseText); }
//};
//xhr.send();
$.ajax({
url: url,
type: type,
crossDomain:true,
cors:true,
success: function(data){
$(id).append(data);
},
error: function(data) {
console.log('ERROR '+url);
console.log(data);
$(id).append(getError(url));
}
});
getData(location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '')+'/modules/mymodule', 'text/html', 'GET', '#content');
看起來這兩個請求都是HTTP或HTTPS(不是混合)。是這樣嗎?是否有任何重定向? (其實,它看起來像你正在向*相同的*起源請求,爲什麼你只是使用相對URL?) – Quentin
你在測試什麼瀏覽器?如果你嘗試不同的瀏覽器,你會得到一個不同的(也許更有啓發性的)錯誤信息? – Quentin
>你爲什麼只使用相對URL? 嗡嗡聲..這是一個很好的問題..並沒有理由... 剛剛在相對測試:getData('/ modules/mymodule','text/html','GET','#content'); 和我有相同的結果。 –