根据 chrome 扩展 API 跨域调用使用 XMLHttpRequest 对象,如果设置了权限,则应该被允许:
扩展可以与其源之外的远程服务器通信,只要它首先请求跨源权限。
我正在密切关注教程,但下面的代码给了我一条错误消息:
XMLHttpRequest 无法加载http://www.google.com/search?hl=en&q=ajax。Access-Control-Allow-Origin 不允许 Origin chrome-extension://bmehmboknpnjgjbmiaoidkkjfcgiimbo。
我不仅允许向 google.com 发出请求,还允许向任何网站发出请求,但仍然无法通过。有人可以帮忙吗?
我的清单文件:
{
"name": "The popup",
"version": "0.1",
"popup": "popup.html",
"permissions": [
"http://*/*",
"https://*/*",
"https://www.google.com/*",
"http://www.google.com/*"
],
"browser_action": {
"default_icon": "clock-19.png",
"default_title": "This is title",
"default_popup": "popup.html"
}
}
实际调用:
function sendRequest() {
document.write("Sending request");
var req = new XMLHttpRequest();
req.open("GET", "http://www.google.com/search?hl=en&q=ajax", true);
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
alert(req.responseText);
document.write("OK");
}
}
};
req.send();
}