我想在用户点击按钮时从Chrome扩展程序制作XHR。然后我想关闭窗口。
如果我在发送后关闭窗口,请求将被取消。如果我关闭请求侦听器中的窗口,则扩展程序崩溃。
如何关闭窗口?
var requestListener = function() {
bkg.console.log(this.readyState);
window.close() // <-- When I put this here, the extension crashes.
};
document.addEventListener('DOMContentLoaded', function() {
var button = document.getElementById('button');
button.addEventListener('click', function() {
var data = {foo: "bar"};
var xhr = new XMLHttpRequest();
xhr.open("post", "http://localhost:8080", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = requestListener;
xhr.send(JSON.stringify(data));
window.close(); // <-- When I put this here, the request is canceled.
});
});
答案 0 :(得分:-1)
一个疯狂的猜测:也许窗口太快关闭,所以像......
<body onbeforeunload="SendFunction();">
......可能有所帮助。祝你好运。