当我使用jQuery ajax
或XMLHttpRequest
在 background.js 中发送请求时,我收到了错误消息:
background.js:51 GET chrome-extension://kameliefngmopmegikbnifbclhkihkjm/http%3A%2F%2Fhtcjy.com%2Fmobile%2Fshop%2Fm%2Fg%2Fgoods-1.do%3F net::ERR_FILE_NOT_FOUND
为什么在我的请求网址之前添加extension://kameliefngmopmegikbnifbclhkihkjm/
?
我能为错误做些什么。
这是我的代码:
background.js
chrome.webRequest.onBeforeRequest.addListener(function(details) {
chrome.tabs.query({active:true},function(tab){
var method = "get";//details.method;
var actualUrl = "http%3A%2F%2Fhtcjy.com%2Fmobile%2Fshop%2Fm%2Fg%2Fgoods-1.do%3F"; //details.url;
sendCrossOrigin(actualUrl, method, function(responseText){
console.log(responseText);
})
});
}, {urls: ["*://*/mock/requestOnServer.do?*"]}, ["blocking"] );
function sendCrossOrigin(url, type, success){
var xhr = new XMLHttpRequest();
xhr.open(type, url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
// innerText does not let the attacker inject HTML elements.
//documemt.getElementById("resp").innerText = xhr.responseText;
//var resp = JSON.parse(xhr.responseText);
success(xhr.responseText);
}
}
xhr.send();
}
的manifest.json
{
"name": "RAP",
"version": "0.1",
"manifest_version": 2,
"description": "RAP Chrome Plugin for Cross-domain",
"browser_action": {
"default_icon": "icon.png",
"default_title": "RAP",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"*://htcjy.com/*",
"*://*/*",
"webRequest",
"webRequestBlocking"
],
"background": {
"scripts": ["jquery-2.0.0.min.js", "background.js"]
},
"content_scripts":[{
"matches":["http://*/*"],
"js":["jquery-2.0.0.min.js", "content_script.js"]
}]
}
答案 0 :(得分:1)
我已经解决了。
这是因为我发布了像http%3A%2F%2Fhtcjy.com%2Fmobile%2Fshop%2Fm%2Fg%2Fgoods-1.do%3F
但我写了
"permissions": [
"tabs",
"*://htcjy.com/*",
"*://*/*",
"webRequest",
"webRequestBlocking"
]
它无法匹配网址,因此我调用了函数decodeURIComponent
来转换为网址http://htcjy.com/mobile/shop/m/g/goods-1.do?