librelist archives

« back to archive

Mongrel2 as a Cross Domain Ajax Server

Mongrel2 as a Cross Domain Ajax Server

From:
gustavo giudici
Date:
2011-09-23 @ 11:39
Hello all,

I'm trying to use Mongrel2 as a backend server from where several web sites
will fetch data trough clients making ajax calls. To make this possible I
need to be able to set the Access-Control-Allow-Origin HTTP header as
Cross-Origin Resource Sharing dictates.

Is it possible to add it to every response, like in apache when I declare
"Header set Access-Control-Allow-Origin *" in the httpd.conf?
Or should I set in every handlers something like a
response.headers.set('Header set Access-Control-Allow-Origin', '*')?
Should I put it on req.headers.get() instead?

Thanks!

Gustavo G

Re: [mongrel2] Mongrel2 as a Cross Domain Ajax Server

From:
Loic d'Anterroches
Date:
2011-09-23 @ 12:49
Hello,

> I'm trying to use Mongrel2 as a backend server from where several web sites
> will fetch data trough clients making ajax calls. To make this possible I
> need to be able to set the Access-Control-Allow-Origin HTTP header as
> Cross-Origin Resource Sharing dictates.
> 
> Is it possible to add it to every response, like in apache when I declare
> "Header set Access-Control-Allow-Origin *" in the httpd.conf?
> Or should I set in every handlers something like a
> response.headers.set('Header set Access-Control-Allow-Origin', '*')?
> Should I put it on req.headers.get() instead?

Yes, you need to output the header yourself at the handler level. The
way you do it will depend of the language/framework you use. If you use
Python/Django you could just use a middleware to set the header for each
answer. Usually the frameworks are pretty good at providing a way to
modify all the outgoing answers in a systematic way.

loïc

--
Indefero - Project management and code hosting - http://www.indefero.net
Photon - High Performance PHP Framework - http://photon-project.com
Céondo Ltd - Web + Science = Fun - http://www.ceondo.com

Re: [mongrel2] Mongrel2 as a Cross Domain Ajax Server

From:
gg
Date:
2011-09-23 @ 13:16
Thanks Loic.

I just had to put

conn.reply_http(req, response ,200,"OK",{"Access-Control-Allow-Origin": 
"*"})

in the handler.

Long live mongrel2, zeromq!

Cheers,
Gustavo g


On 23/09/11 14:49, Loic d'Anterroches wrote:
> Hello,
>
>> I'm trying to use Mongrel2 as a backend server from where several web sites
>> will fetch data trough clients making ajax calls. To make this possible I
>> need to be able to set the Access-Control-Allow-Origin HTTP header as
>> Cross-Origin Resource Sharing dictates.
>>
>> Is it possible to add it to every response, like in apache when I declare
>> "Header set Access-Control-Allow-Origin *" in the httpd.conf?
>> Or should I set in every handlers something like a
>> response.headers.set('Header set Access-Control-Allow-Origin', '*')?
>> Should I put it on req.headers.get() instead?
> Yes, you need to output the header yourself at the handler level. The
> way you do it will depend of the language/framework you use. If you use
> Python/Django you could just use a middleware to set the header for each
> answer. Usually the frameworks are pretty good at providing a way to
> modify all the outgoing answers in a systematic way.
>
> loïc
>
> --
> Indefero - Project management and code hosting - http://www.indefero.net
> Photon - High Performance PHP Framework - http://photon-project.com
> Céondo Ltd - Web + Science = Fun - http://www.ceondo.com