Javascript 未选中的runtime.lastError:在收到响应之前消息端口已关闭
我正在尝试编写一个chrome扩展来显示响应头,特别是安全响应头。我试图通过XMLHTTPRequests执行此操作,但它返回一个错误Javascript 未选中的runtime.lastError:在收到响应之前消息端口已关闭,javascript,ajax,google-chrome-extension,Javascript,Ajax,Google Chrome Extension,我正在尝试编写一个chrome扩展来显示响应头,特别是安全响应头。我试图通过XMLHTTPRequests执行此操作,但它返回一个错误未检查的运行时。lastError:消息端口在收到响应之前关闭。 function headerUpdate(){ //run all of the other functions for security here to update an array //then update the headers present column on the pop
未检查的运行时。lastError:消息端口在收到响应之前关闭。
function headerUpdate(){
//run all of the other functions for security here to update an array
//then update the headers present column on the popup with the array values
var url = document.URL;
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.send();
request.onreadystatechange = function() {
if(this.readyState == this.HEADERS_RECEIVED) {
// Get the raw header string
var headers = request.getAllResponseHeaders();
// Convert the header string into an array
// of individual headers
var arr = headers.trim().split(/[\r\n]+/);
// Create a map of header names to values
var headerMap = {};
arr.forEach(function (line) {
var parts = line.split(': ');
var header = parts.shift();
var value = parts.join(': ');
headerMap[header] = value;
});
}
return arr;
}
}
如果在onMessage中调用异步XHR,其回调应该是。我在onMessage的末尾返回了true,错误没有改变。您还需要使用sendResponse(arr)而不是返回arr。谢谢!这就解决了!如果在onMessage中调用异步XHR,其回调应该是。我在onMessage的末尾返回了true,错误没有改变。您还需要使用sendResponse(arr)而不是返回arr。谢谢!这就解决了!