2

I am trying to POST to my cross domain *Rest service* via javascript, and realized that's not possible unless I use this specfication;

http://www.w3.org/wiki/CORS_Enabled

But there is very limited documentation how I can implement this, I have a few questions;

1- I use Glassfish Jee6 app server so can I simply add a jetty filter to my web.xml and job is done?

2- And for client side(mobile website), is there any javascript library which helps to implement this specification? Can I still use the existing ajax functions in JQuery or I need something more specific?

Spring
  • 11,333
  • 29
  • 116
  • 185
  • 1
    why dont you make a server side proxy, have your client code query the proxy and get the results back? – Rafay Jan 06 '13 at 18:58
  • 1
    @3nigma I thought about it but 1- I read that it eats up resources-both processing power, bandwidth. 2- I have no clue how can I do that :) – Spring Jan 06 '13 at 19:00
  • for jetty there is a cross origin filter or you can just add Access-Control-Allow-Headers: * and for jQuery you can do jsonp and do jQuery.support.cors = true; for opera etc – Karussell Jan 06 '13 at 19:01
  • @Karussell isnt jsonp for only GET? I need POST – Spring Jan 06 '13 at 19:04

1 Answers1

1

Jetty filter:

<web-app>
 <filter>
   <filter-name>cross-origin</filter-name>
   <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
   <init-param>
       <param-name>allowedOrigins</param-name>
       <param-value>*</param-value>
   </init-param>
   <init-param>
       <param-name>allowedMethods</param-name>
       <param-value>*</param-value>
   </init-param>
   <init-param>
       <param-name>allowedHeaders</param-name>
       <param-value>*</param-value>
   </init-param>
 </filter>
 <filter-mapping>
     <filter-name>cross-origin</filter-name>
     <filter-pattern>*</filter-pattern>
 </filter-mapping>
</web-app>
Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50
  • tnx do I ALSO have to add Access-Control-Allow-Headers in the code? or this is eneough? – Spring Jan 06 '13 at 19:05
  • http://stackoverflow.com/questions/5750696/how-to-get-a-cross-origin-resource-sharing-cors-post-request-working do you think answer given to this comforms to this filter situation? – Spring Jan 06 '13 at 19:16
  • The filter config I gave you allows all headers and all methods from all origins. – Werner Kvalem Vesterås Jan 06 '13 at 19:20