AJAX calls not completing because the popup closes
I am using some AJAX calls in my popup on a but开发者_运维问答ton click:
var xhr = new XMLHttpRequest();
xhr.open("GET", yourURL, true);
xhr.send();
chrome.tabs.create({url:<some url here>});
The problem is that the popup closes as soon as the button is clicked, and I think thats why the ajax calls are not completing.
Is this a common issue? What can I do to make sure that the ajax calls have sufficient time to complete?
This is what a background page was made for. Move your code to a background page into some myFunction()
, then call it from a popup:
chrome.extension.getBackgroundPage().myFunction()
You need a callback after the request has finished.
var xhr = new XMLHttpRequest();
xhr.open("GET", yourURL, true);
xhr.onreadystatechange = function() {
if (xhr.readyState===4 && xhr.status===200) {
chrome.tabs.create({url:<some url here>});
}
}
xhr.send();
精彩评论