I'm trying to make a request from a background page and failing with
the error XMLHttpRequest cannot load ... Origin chrome-extension://...
is not allowed by Access-Control-Allow-Origin.
Here's what my setup looks like. The relevant parts from
manifest.json:
"background_page" : "background.html",
"permissions": [
"tabs",
"
http://localhost:3000/*"
],
"content_scripts": [
{
"matches": ["http://*/*"],
"js" : ["test.js"]
}
]
And here's the relevant part from background.html:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
callback(xhr.responseText);
} else {
callback(null);
}
}
}
var url = '
http://localhost:3000/api/v1/log_event';
xhr.open('POST', url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({"foo" : "bar"}));
I see on the server that it gets the OPTIONS request before hand.
Here's what that response looks like via curl:
curl -X OPTIONS -v "
http://localhost:3000/api/v1/log_event"
* About to connect() to localhost port 3000 (#0)
* Trying ::1... Connection refused
* Trying fe80::1... Connection refused
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 3000 (#0)
> OPTIONS /api/v1/log_event HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8l zlib/1.2.3
> Host: localhost:3000
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html
< Access-Control-Allow-Origin: *
< Access-Control-Allow-Methods: POST, OPTIONS
< Access-Control-Allow-Headers: Content-Type
< X-Ua-Compatible: IE=Edge
< X-Runtime: 0.005100
< Content-Length: 1
< Server: WEBrick/1.3.1 (Ruby/1.9.2/2010-12-25)
< Date: Sat, 26 Feb 2011 18:41:53 GMT
< Connection: Keep-Alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
So the flow is that I see the OPTIONS request on the server, then I
see a POST with the data that I was expecting, and then in the console
of the background.html I get that error. Further, ready state change
in the XMLHttpRequest gets called with 4. However, the status code is
0.
Anyone have any ideas why this isn't working?
Thanks,
Paul