我正在尝试使用以下代码修改Chrome扩展程序中的响应标头:
var modifyResponse = function(details) {
var responseHeaders = [];
responseHeaders.push({name: "Access-Control-Allow-Credentials", value: "true"});
responseHeaders.push({name: "Access-Control-Allow-Origin", value: "*"});
responseHeaders.push({name: "Access-Control-Allow-Methods", value: "OPTIONS, GET, POST"});
responseHeaders.push({name: "Access-Control-Allow-Headers", value: "Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control"});
details.responseHeaders = details.responseHeaders.concat(responseHeaders);
return {responseHeaders: details.responseHeaders};
};
chrome.webRequest.onHeadersReceived.addListener(modifyResponse, {urls: ["<all_urls>"]}, ["responseHeaders", "blocking"]);
在我的manifest.json
文件中,我有:
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
但是,在Chrome检查器中检查响应标头时,这些标头不会显示。我可以确认正在调用modifyReponse
方法,但我无法确认标题是否显示。我是否遗漏了许可或者我的代码是错误的还是这是扩展的限制?
更新
通过http://api.jquery.com/
运行测试AJAX调用:
$.get("http://api.jquery.com/", {}, function(data, status, jqXHR) { console.log(jqXHR.getAllResponseHeaders()); });
我得到了正确的响应标头,这表明标头已插入,但它们仍未显示在请求检查器中。