处理由Chrome扩展程序引起的AJAX来源错误 [英] Handling AJAX Origin errors, caused by the Chrome Extension
问题描述
拒绝
的加载chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json。
必须在web_accessible_resources清单键
中列出资源,以便通过扩展名外的页面加载。
我试过这个:
$。ajax({
url:'chrome-extension:// kldbdjcbjohfhddpicldkbifbkcdanid / data .json',
datatype:'json',
成功:function(xhr){
alert('ok');
},
erro r:function (xhr,status,err){
alert('status:'+ xhr.status +' - '+ err);
}
});
但没有任何变数。错误状态为0,错误消息为空。
听起来就像您试图从内容脚本加载此资源,基于错误。
您必须通过 web_accessible_resources
。您的错误消息指出这是解决方案。
完成此操作后,您可以使用 chrome.extension.getURL(resourcePath)
。
以下是我的清单摘录:
web_accessible_resources:[
* .html
以下是我用来请求HTML的代码template file:
var url = chrome.extension.getURL(template.html);
。.get(url,function(data,textStatus,jqXHR){
console.log(Got the template!);
console.log(data);
}, HTML);
Is there way to handle AJAX Origin errors from Chrome's extensions ?
Denying load of chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json. Resources must be listed in the web_accessible_resources manifest key in order to be loaded by pages outside the extension.
I tried this:
$.ajax({
url: 'chrome-extension://kldbdjcbjohfhddpicldkbifbkcdanid/data.json',
datatype: 'json',
success: function(xhr) {
alert('ok');
},
erro r: function(xhr, status, err) {
alert('status: ' + xhr.status + ' - ' + err);
}
});
but got nothing in variables. error status is "0", error message is empty.
Sounds like you are attempting to load this resource from a content script, based on the error.
You have to explicitly add either the individual extension resources, or patterns that match your extension resources, in the manifest via web_accessible_resources
. Your error message states this is the solution.
Once you've done this, you can construct the URL using chrome.extension.getURL(resourcePath)
.
Here's an excerpt from my manifest:
"web_accessible_resources" : [
"*.html"
]
And here's the code I'm using to request my HTML template file:
var url = chrome.extension.getURL("template.html");
$.get(url,function(data, textStatus, jqXHR){
console.log("Got the template!");
console.log(data);
},"html");
这篇关于处理由Chrome扩展程序引起的AJAX来源错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!