Getting Error: XMLHttpRequest cannot load ... Origin chrome-extension://... is not allowed by Access-Control-Allow-Origin

2,699 views
Skip to first unread message

Paul Dix

unread,
Feb 26, 2011, 1:47:21 PM2/26/11
to Chromium-extensions
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

Paul Dix

unread,
Feb 26, 2011, 2:44:50 PM2/26/11
to Chromium-extensions
Found it. I'll answer my own question so desperate searchers that find
this later will have the answer. Just had to update the permissions
section of the manifest to look like this:

"permissions": [
"tabs",
"http://localhost/"
],

The problem was that I had the port in there. It's also odd that if I
didn't have the localhost:3000 in there it wouldn't even get past the
first OPTIONS request and try the POST. So with the port it kind of
half worked and without it fully worked. Kind of surprised that that's
the behavior, but whatevs. Fixed now!

Paul

Arne Roomann-Kurrik

unread,
Feb 28, 2011, 1:19:22 PM2/28/11
to Paul Dix, Chromium-extensions
Thanks for following up with the answer!

~Arne

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Reply all
Reply to author
Forward
0 new messages