2015-04-16 126 views
1

我創建safari擴展,我在這個擴展中注入js。在這個JS代碼中,我發送ajax調用,在控制檯中創建以下錯誤。 「請求頭域X-Requested-With不允許通過訪問控制允許頭」 這裏是我的代碼:需要幫助來解決Ajax問題?

這個函數我從網上覆制來解決跨域問題,但它不工作請幫我弄清楚這一點。

function createCORSRequest(method, url) { 

    var xhr = new XMLHttpRequest(); 
    if ("withCredentials" in xhr) { 
     // XHR has 'withCredentials' property only if it supports CORS 
     xhr.open(method, url, true); 
    } else if (typeof XDomainRequest != "undefined") { // if IE use XDR 
     xhr = new XDomainRequest(); 
     xhr.open(method, url); 
    } else { 
     xhr = null; 
    } 
    return xhr; 
} 


var request = createCORSRequest("get", "https://www.karmora.com/list.xml"); 

if (request) { 
    // Define a callback function 
    request.onload = function() { 
}; 
    // Send request 
    request.send(); 
} 

$.get('https://example.com', function (data) { 
    alert("Ajax call successfull"); 
}); 

回答

1

你的問題與Same-origin_policy

相關。如果你有機會到服務器,添加到Apache Web服務器的虛擬主機配置以下設置:

Header set Access-Control-Allow-Origin "*" 
Header set Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept" 
+1

我在Chrome擴展使用相同的代碼它的工作正常。錯誤只是在Safari瀏覽器。 –