【发布时间】:2013-02-05 00:03:48
【问题描述】:
我正在尝试在 Chrome 扩展程序中使用 XHR 从 Yahoo 获取天气信息:
$.ajax({
url: "https://weather.yahooapis.com/forecastrss?w=" + 250226 + "&u=c",
dataType: 'xml',
success: function(data) {
console.log(data);
}
});
并且我已请求使用此脚本跨域的权限:
$("button").click(function(){
chrome.permissions.request({
origins: ['*://weather.yahooapis.com/*']
}, function(granted) {
if (granted) {
console.log("Success creating permission."); //successful
} else {
console.log("Not successful.");
}
});
但是,它仍然给我一个错误提示:
XMLHttpRequest cannot load http://weather.yahooapis.com/forecastrss?w=2502265&u=c. Origin chrome-extension://randomid is not allowed by Access-Control-Allow-Origin.
而且我想不出发生这种情况的任何原因。有什么想法吗?
【问题讨论】:
-
我不是一个网络开发人员,but the documentation 似乎没有指定通配符是可以的。
-
@ta.speot.is - developer.chrome.com/extensions/declare_permissions.html
-
我可能不能再贡献了(不是 Web 开发人员等),但是说“匹配模式”的文档似乎是针对
permissions而不是origins。 -
@ta.speot.is - “匹配模式”适用于
origins,因为在permissions中只能放入权限列表。即使我删除了*,它仍然无法正常工作。 :( -
考虑到this sample extension by Google,你能试试
http://weather.yahooapis.com/(没有*)吗?
标签: javascript google-chrome google-chrome-extension xmlhttprequest