http.post(url,postData,options)不设置标头

时间:2016-05-26 13:07:38

标签: post angular http-headers

在以下代码中使用HTTP post方法时遇到问题:

    let body = JSON.stringify(applicationLink);
    let requestHeaders = new Headers();

    var headers = new Headers();

    headers.set('Content-Type',  ['application/json']);
    headers.set('Access-Control-Allow-Origin', ['*']);

    let reqoptions =  new RequestOptions({
        headers: headers
    });



    return this._http.post(this._applicationLinksUrl + this._linkServicePath,body,reqoptions).map(res => res.json())

当我执行代码时,我看到一个错误:

XMLHttpRequest cannot load http..... Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http....' is therefore not allowed access.

Request Method:OPTIONS is mde instead of post
Request URL:http://localhost:7001/workprocess-service/resources/links
Request Method:OPTIONS
Status Code:200 OK
Remote Address:[::1]:7001
Response Headers
view source
Allow:OPTIONS,POST,GET,HEAD
Content-Type:application/vnd.sun.wadl+xml
Date:Thu, 26 May 2016 11:10:25 GMT
Last-Modified:Thu, 26 May 2016 09:02:27 CEST
Transfer-Encoding:chunked
Vary:Accept
X-Powered-By:Servlet/3.0 JSP/2.2
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:access-control-allow-origin, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:7001
Origin:http....
Referer:http....
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36

并且我的数据未与请求相关联。

如果我删除reqoption

return this._http.post(this._applicationLinksUrl + this._linkServicePath,body).map(res => res.json())

请求是POST但我收到错误415(不支持的媒体类型)

Request URL:http://localhost:7001/workprocess-service/resources/links
Request Method:POST
Status Code:415 Unsupported Media Type
Remote Address:[::1]:7001
Response Headers
view source
Connection:close
Content-Length:22
Content-Type:text/html; charset=UTF-8
Date:Thu, 26 May 2016 11:14:44 GMT
X-Powered-By:Servlet/3.0 JSP/2.2
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:48
Content-Type:text/plain;charset=UTF-8
Host:localhost:7001
Origin:http:....
Referer:http:.....
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36
Request Payload
view source
{labelFR: "dsds", labelNL: "dsds", url: "dsds"}
labelFR
:
"dsds"
labelNL
:
"dsds"
url
:
"dsds"

我的数据已链接到请求

1 个答案:

答案 0 :(得分:1)

您需要让服务器设置标头Access-Control-Allow-Origin,因为您正在发送CORS“跨源资源共享”请求。这只是意味着您将请求发送到与您发送的网址不同的网址。

我看到您的后端是servlet/3.0,您可以通过添加网络过滤器为所有来源启用CORS

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;


@WebFilter(urlPatterns = {"*"})
public class CORSFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse res = (HttpServletResponse) response;

        res.addHeader("Access-Control-Allow-Origin", "*");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
    }
}