根据chrome扩展API,如果设置了权限,则应允许使用XMLHttpRequest对象进行跨域调用:
扩展可以与其来源之外的远程服务器通信,只要它首先请求跨源权限即可.
我正在密切关注本教程,但下面的代码给出了一条错误消息:
XMLHttpRequest无法加载http://www.google.com/search?hl=en&q=ajax.原始chrome-extension:// bmehmboknpnjgjbmiaoidkkjfcgiimbo不允许使用Access-Control-Allow-Origin.
我不仅允许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"
}
}
Run Code Online (Sandbox Code Playgroud)
实际的电话:
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();
}
Run Code Online (Sandbox Code Playgroud) javascript ajax google-chrome xmlhttprequest google-chrome-extension