Chrome Extension jQuery POST callback
I have been successful calling a jQuery POST from the popup.html in Google Extensions. However, the success or error callback functions have not been called. I've searc开发者_C百科hed around on stackoverflow, and it seems that this is because of domain.
As a solution, I've tried to send a chrome.extension.sendRequest to the background.html page and have the background page execute the post instead. However, the listener on the background.html page doesn't seem to receive the request.
In popup.html, I have
chrome.extension.sendRequest({test:'test123'}, function(response) {
alert('hello')
}
In background.html, I have
chrome.extension.onRequest.addListener(
function(request, sender, resndResponse) {
console.log(request)
}
)
Why is this not working? Is there another solution?
It doesn't work because alerts is not a good way of debugging extensions. Replace all alerts with console.log
, especially in popup where it simply silently stops the rest of code from executing.
Popup and background pages have exactly the same privileges, so whatever works or not in popup would behave exactly the same in background page.
So, start with removing all alerts and make sure you have declared domain permissions in the manifest:
"permissions": [
"http://destination.com/"
],
That should be enough, assuming the rest of the code is correct.
Thanks for all the help. Apparently I was stupid and the reason the $.post()
wasn't calling the success function was that I had Tornado sending back a string instead of a JSON object...
精彩评论