/*set the response header*/
Form responseHeaders = (Form) getResponse().getAttributes().get("org.restlet.http.headers");
if (responseHeaders == null) {
responseHeaders = new Form();
responseHeaders.add("Access-Control-Allow-Origin", "*");
responseHeaders.add("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
getResponse().getAttributes().put("org.restlet.http.headers", responseHeaders);
}
I added this in my restlet 2.0 code to allow for cross domain access, this does make the first GET to work on page load, but when I try to do POST later(with backbone model.save()), browser sends Options with a null entity instead.
It does send the right POST if I did not add the code above
This happens on Opera, Firefox, and Chrome (works fine if I start chrome with --disable-web-security), so i assume it is still a browser security issue, could anyone provide explanations on why this would happen and what might be the fix?
Below code can be used to resolve cross domain issue in Restlet 2.0
From What is the HTTP OPTIONS method?
I have fought several times with CORS issues and I have always solved them with the method of try and error my suggestion for your case is add
OPTIONS
to your Allow Methods :And make your server to respond to
OPTIONS
request with an*
, in Sinatra is like this:Update
For the new issue explained in the comment about the error
header field Content-Type is not allowed by Access-Control-Allow-Headers.
Try to add another CORS header:
On a different content the code give below works for Restlet 2.1 upwards.
}
Thanks for fguillen's advice, have some googling I found the way that would work
so this is the right code to inject for cors in Restlet 2.0 do not miss responseHeaders.add("Access-Control-Allow-Headers", "Content-Type"); at least when you are dealing with backbone
Perhaps this link could give you some hints about how CORS works and its support in Restlet with the
CorsFilter
:Hope it helps you, Thierry
Slightly off topic but I thought it might be useful for somebody and save time.
I was having CORS issues with uwsgi server and backbone. This is part of my uwsgi.ini file which seem to work for POSTing.
As mentioned by fguillen server needs to reply '*' for OPTIONS request.