I try to load a json file from a server with XMLHttpRequest.
But I only get status==0 and the chrome dev console says: "(canceled)"
I can reproduce this with this (simplified) code:
var httpReq = new window.XMLHttpRequest();
httpReq.onreadystatechange = function(data) {
if (httpReq.readyState == 4) {
alert(httpReq.status);
}
}
httpReq.open("GET", "http://dl.dropbox.com/[...]/data.json");
httpReq.send(null);
This code stops working, as it is in the extension context.
The same function is also used to load files from an other server (also crossdomain), without any problem.
Here is the manifest part:
"permissions": [ "http://theOtherWorkingServer.com/*", "http://dl.dropbox.com/*", "https://dl.dropbox.com/*" ],
I've read a lot about this problem, but found no working solution.
(The main page is not reloading and also not redirected at this time)
Any suggestions??
解决方案
The error you're getting is a sign that a redirect was performed, to an URL which does not match any of your declared permissions.
To verify my statement, add the *://*/* or permission to the manifest file, and you will notice that the request completes successfully.
To determine which redirects are followed, follow the following steps:
Open the developer tools, go to the Network tab.
Paste the URL in the omnibox, and hit enter
Read the URLs in the developer tools.
As you can see in the previous picture, dl.dropbox.com actually redirects to dl.dropboxusercontent.com. This domain has to be added to the permission section of your manifest file, to fix the problem:
"permissions": [
"http://theOtherWorkingServer.com/*",
"*://dl.dropbox.com/*",
"*://dl.dropboxusercontent.com/*"
],
(the the wildcard at the scheme matches http and https - see match patterns)