简体   繁体   English

CORS angular js + restEasy on POST

[英]CORS angular js + restEasy on POST

I'm doing some POST requests from my angular js app to my RESTful API implemented using RestEasy . 我正在从我的angular js应用程序到使用RestEasy实现的RESTful API进行一些POST请求。
The case is that I need CORS so I added a servlet filter with this code: 这种情况是我需要CORS,所以我用以下代码添加了一个servlet过滤器:

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletResponse response = (HttpServletResponse) res;
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.addHeader("Access-Control-Max-Age", "3600");
    response.addHeader("Access-Control-Allow-Headers", "Content-Type");
    chain.doFilter(req, res);
}

But I can't figure out why it works only with GET requests and not POST requests, the error on chrome's console is: 但是我不知道为什么它仅适用于GET请求而不适用于POST请求,chrome控制台上的错误是:

No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“ Access-Control-Allow-Origin”标头

My POST request is: 我的POST请求是:

$http({method: 'POST', 
       url: myUrl,
       data: $scope.data,
       headers: {'Content-Type': 'application/json'}
});  

This is the reponse I receive on POST: 这是我在POST上收到的回复:

Allow:POST, OPTIONS
Content-Length:0
Date:Thu, 03 Apr 2014 23:27:22 GMT
Server:Apache-Coyote/1.1

Any Idea? 任何想法? Thanks! 谢谢!
EDIT: 编辑:
Tested on IE10 and it works but doesn't work on chrome neither firefox ... any body knows why? 经过IE10的测试,它可以工作,但不能在chrome上工作,也不能在Firefox上工作……任何人都知道为什么?

Well finally I came to this workaround: 好吧,最后我来到了这种解决方法:
The reason it worked with IE is because IE sends directly a POST instead of first a preflight request to ask for permission. 它与IE配合使用的原因是因为IE直接发送POST而不是先发送预检请求以请求许可。
But I still don't know why the filter wasn't able to manage an OPTIONS request and sends by default headers that aren't described in the filter (seems like an override for that only case ... maybe a restEasy thing ...) 但是我仍然不知道为什么过滤器无法管理OPTIONS请求并默认发送过滤器中未描述的标头(似乎仅在这种情况下覆盖...也许是restEasy的事情.. 。)

So I created an OPTIONS path in my rest service that rewrites the reponse and includes the headers in the response using response header 因此,我在我的rest服务中创建了一个OPTIONS路径,该路径重写了响应并使用响应标头将标头包含在响应中

I'm still looking for the clean way to do it if anybody faced this before. 如果有人以前遇到过这种情况,我仍在寻找一种干净的方法。

I have had good luck configuring Cross-origin resource sharing (CORS) for my API (on Wildfly) by using this lib: 我很幸运通过使用此lib为我的API(在Wildfly上)配置跨域资源共享(CORS):

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

It's very easy to setup. 设置非常简单。 Just add the above dependency to your pom and then add the following config to the webapp section of your web.xml file. 只需将上述依赖项添加到pom中,然后将以下配置添加到web.xml文件的webapp部分。

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

    <init-param>
        <param-name>cors.allowGenericHttpRequests</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>

    <init-param>
        <param-name>cors.allowSubdomains</param-name>
        <param-value>false</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, HEAD, POST, DELETE, OPTIONS</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>*</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>cors.maxAge</param-name>
        <param-value>3600</param-value>
    </init-param>

</filter>

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

You can also configure it with a properties file instead if you prefer. 如果愿意,还可以使用属性文件来配置它。 This lib works like a charm and gives you a lot of configuration flexibility! 该库的工作原理很吸引人,为您提供了许多配置灵活性!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM