我正在为一个需要访问minus.com的Chrome扩展编写代码,它使用test.js 2.0进行身份验证,所以我编写了一个javascript文件“test.js”,然后将其包含在oAuth文件“test.html”中,然后在Chrome中加载“test.html”以测试用于身份验证的javascript代码。
“test.js”的结构如下所示:
function Ajax(url, options) {
// some function content
// Sending data
if (options.method === "POST" && (options.params || options.binaryData)) {
if (options.binaryData) {
xhr.sendAsBinary(options.binaryData);
} else {
xhr.send(hashToQueryString(options.params));
}
} else {
xhr.send(null);
}
return xhr;
}
function refreshToken(refresh_token) {
var params = {
'grant_type': 'refresh_token',
'client_id': API_KEY,
'client_secret': API_SECRET,
'refresh_token': refresh_token,
'scope': 'read_all modify_all upload_new'
}
new Ajax("https://minus.com/oauth/token", {
params: params,
onSuccess: function(response) {
console.log(response.access_token);
},
onError: function(response) {
console.log('error: wrong_token');
}
});
}
refreshToken();当我在Chrome中加载'test.html‘来测试'test.js’时,它在控制台中提示一个错误,说"XMLHttpRequest无法加载https://minus.com/oauth/token?...原始文件://是访问控制允许原始文件不允许的。“我试着用"--allow-file-access-from-files“或者"--disable-web-security”来启动Chrome,但是没有解决问题。但是,如果我注释了“Ajax”函数中的“发送数据”部分,就不会出现错误。
有谁知道这是怎么回事吗?
谢谢!
发布于 2012-03-23 17:03:19
您可以通过使应用程序成为支持跨域调用的打包应用程序来解决此问题。托管的应用程序不需要。
发布于 2012-03-23 17:27:43
具体来说,您需要将希望能够在跨域中访问的主机添加到扩展清单中:
http://code.google.com/chrome/extensions/xhr.html
{
"name": "My extension",
...
"permissions": [
"https://*.minus.com/"
],
...
}编辑
顺便说一句,当我有很多活动的扩展时,我的chrome有时会出现奇怪的跨域错误。然后,我必须禁用至少几个,刷新扩展程序,它就可以工作了-或者有时重启chrome。
https://stackoverflow.com/questions/9833641
复制相似问题