java - Restlet 1.1 Access Control Header Issue -
i'm working on restlet built on restlet 1.1.1
the issue i'm facing setting 'access-control-allow-origin' header allow cross domain requests. i've attempted few things didn't work.
method one, put header in acceptrepresentation function:
@override public void acceptrepresentation( representation resetentity ) { form headers = (form)getresponse().getattributes().get("org.restlet.http.headers"); if (headers == null) { headers = new form(); getresponse().getattributes().put("org.restlet.http.headers", headers); } headers.add("access-control-allow-origin","https://origin.server.edu"); //other code here actual resource logic... }
this didn't work. still received errors when attempting send request using jquery such:
jquery.ajax({ type: "post", contenttype: "application/json", url: "https://test.servername.edu/cas/cas-rest-api/reset/", data: json.stringify("{\"uname\" : \"someone\", \"attr\":\"dataelement\" }"), datatype: "json", crossdomain: true }) .done(function(data){ console.log("success"); alert(data); }) .fail(function(data){ console.log("failure"); console.log(data); alert(data); });
this didn't work. noticed init function in resource class. figured i'd attempt putting code there see if change situation.
@override public void init(context context, request request, response response ){ form headers = (form)response.getattributes().get("org.restlet.http.headers"); if (headers == null) { headers = new form(); response.getattributes().put("org.restlet.http.headers", headers); } headers.add("access-control-allow-origin","https://origin.server.edu"); super.init(context, request, response); }
nope. didn't work either. missing here? set header?
thanks replies. after analysis of problem turned out needed configure spring allow option requests restlet in web.xml file shown below:
<servlet> <servlet-name>ccrest</servlet-name> <servlet-class>com.noelios.restlet.ext.spring.restletframeworkservlet</servlet-class> <init-param> <param-name>dispatchoptionsrequest</param-name> <param-value>true</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>
Comments
Post a Comment