Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

jquery - How to enable CORS in jBoss

I am developing an HTML5 application that has to obtain some values from legacy web-services (Jax-Ws) so I use jQuery.soap to query these web services to obtain responses. I have tried the correctness of my requests with SOAP UI, and they work properly.

From my HTML5 client I cannot receive SOAP responses from the server, because in the response there is not the Allow-Control-Allow-Origin header set to *. So, the origin of the request is recognized as not allowed, and the response of the server is an error response.

The message of the error, debugging my HTML5 project with Firebug + Firefox, is:

Locked cross- origin request: the criterion at the origin does not allow the reading of the remote resource. You can solve the problem by moving the resource to the same domain or activating CORS.

How can I enable CORS in jBoss?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to deal with your legacy web-services to fix the issue. As mccannf said above you need to add CORS filter in web.xml.

You can use a solution from thetransactioncompany:

web.xml:

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

maven:

<dependency>
    <groupId>com.thetransactioncompany</groupId>
    <artifactId>cors-filter</artifactId>
    <version>2.5</version>
</dependency>

If you use apache Tomcat you can use built-in CorsFilter:

web.xml:

<filter>
  <filter-name>CorsFilter</filter-name>
  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>CorsFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

pom.xml:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>7.0.42</version>
    <scope>provided</scope>
</dependency>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...