If you still want to use AJAX in this way, (since your web service
must still guard against unauthorized requests that may be spoofing
the origin, or just using Firebug to inject code into your page and
make it "look" authentic, I see these Access Control headers as
absolute garbage), this is the magic incantation we came up with at
Agrosica for Node.js:
// The OPTIONS method to shut Firefox up
function handleOptions(request, response) {
response.writeHead(200, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Method": "POST, GET, OPTIONS",
"Access-Control-Allow-Headers": request.headers["access-
control-request-headers"]
});
response.end();
};
Obviously, you'll need to change the "Access-Control-Allow-Method" to
list whatever HTTP methods you support, and the last line lets Firefox
know that you're perfectly fine with sending whatever headers it
wants, and then you can do whatever you want with them (including
ignore them) -- if you don't allow this, Firefox will complain that it
can't send the request with whatever headers it's asking to send, even
though its perfectly fine with barfing whatever headers it wants to
with "normal" GET/POST requests, including *gasp*, cross-site GET
requests! (Hyperlinks...)
David
On Aug 30, 4:28 am, Damianos Mylonakis <
danmylona...@gmail.com> wrote:
> AJAX is indeeed fobridden to connect to other domains than the one already
> connected to. Thats the security model browsers have. The
> Access-Control-Allow-Origin header wont work on all browsers i think.
> You can use JSONP to bypass the cross-domain limitation, and its supported
> by jquery <
http://api.jquery.com/jQuery.ajax/> (check the dataType jsonp
> option).
>
> for setting a header for every response, check you can use a middleware<
https://groups.google.com/d/msg/express-js/_F8xgznDPno/uRKFkSsrM8kJ>
> .