我已經爲Google Chrome創建了一個簡單的擴展程序,但是我在訪問字典API時遇到了問題。該API運行在我的擴展運行所在的不同域上。 我已閱讀有關此主題的所有StackOverflow線程,但無法解決此問題。Google Chrome擴展程序跨域XMLHttpRequest
我已將API的地址添加到權限。它沒有工作,所以我將其替換爲http://*/*
進行測試。我有以下manifest.json
:
{
"name": "web2memrise",
"version": "0.3",
"manifest_version": 2,
"permissions": ["http://*/*"],
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["jquery.min.js", "contentscript.js"],
"css": ["style.css"]
}],
"web_accessible_resources": ["script.js", "jquery.min.js"]
}
JavaScript函數中,我使API調用:
function translateCallback(json){
var translations = "";
for(var phrase of json){
translations += ", "+phrase.text;
}
translations = translations.substring(2).replace(", ", "\t")
popupContent(translations)
}
function translate(l1, l2, phrase){;
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://deu.hablaa.com/hs/translation/"+phrase+"/"+l1+"-"+l2+"/", true);
xhr.onreadystatechange = translateCallback
xhr.send();
}
但它給我下面的錯誤:
home:19 GET http://nxtck.com/as.php?zid=48360&clbk=nextperf net::ERR_BLOCKED_BY_CLIENTloadScript @ home:19window.onload @ home:37
(index):1 XMLHttpRequest cannot load http://deu.hablaa.com/hs/translation/solution/fra-eng/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.lefigaro.fr' is therefore not allowed access.
script.js:44 Uncaught TypeError: json[Symbol.iterator] is not a function