我正在编写需要访问使用oAuth 2.0进行身份验证的minus.com的Chrome扩展的代码,所以我编写了一个JavaScript文件'test.js',然后将其包含在HTML文件'test.html'中,然后在Chrome中加载'test.html'以测试用于验证的JavaScript代码。Chrome中的错误:“原始文件://不被Access-Control-Allow-Origin允许”
的“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?...原始文件://不被Access-Control-Allow-Origin允许。”我尝试使用“--allow-file-access-from-files”或“--disable-web-security”选项启动Chrome,但它没有解决问题。但是,如果我在“Ajax”函数中评论了“发送数据”部分,则没有错误。
有没有人有想法这里发生了什么?
谢谢!
我相信你正在调用你的请求跨域。如果您这样做,Chrome浏览器将发出此错误。 – Raptor 2012-03-23 03:04:03
那么如何解决这个问题? – chaohuang 2012-03-23 03:07:00