我是在学校学习C++后自学的JavaScript,我认为这是一个很好的做法,试图构建Chrome扩展。我正在尝试访问OpenWeatherMap的API以获取城市ID进行天气搜索。Chrome扩展“Script-src”错误(自学)
这是导致问题的部分代码:
var cityParam = $('#cityInput').val(); //ex. of cityParam = "New York"
var cityURL = "http://api.openweathermap.org/data/2.5/find?callback=?&q="+ cityParam + "&type=like&sort=population&cnt=30";
var cityJSON;
$.getJSON(cityURL, function(data) {
cityJSON = data;
}
我从Chrome中收到了错误:
Refused to load the script [url] ... because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:".
我做了谷歌搜索,它似乎像Chrome浏览器扩展在你可以或不可以做的事情上非常严格(例如:不能嵌入javascript)。对Web开发不太熟悉,我不太确定从哪里开始寻找如何解决这个问题。
的URL返回(我相信)一个JSON,但它有一个额外的开始?(并与结束)。
感谢您的帮助!
编辑:
下面是我所采取的错误的截图。它显示红色突出显示的文本是从jQuery。也许我传入的URL不能被$ .getJSON()处理?
编辑2:
我加入由梅达的建议下我的清单,但错误依然存在:
"permissions": [
"http://api.openweathermap.org/*"
]
这可能也有帮助:http://stackoverflow.com/questions/25867584/extension-refuses-to-load-the-script-due-to-content-security-policy-directive – igaurav
如果您的目标端点支持https ,那么只需在'manifest.json'处添加这一行:'“content_security_policy”:“script-src'self'https://example.com; object-src'self'”' – makerj