Angular2标头未正确设置

时间:2016-10-14 13:51:55

标签: angular http-headers

我正在使用angular2 http。

制作一个简单的帖子请求

问题是它没有正确设置标头。我得到的只是这个

,而不是设置内容类型和授权
{
    "createdAt": "20161013134743",
    "imageURL": "",
    "latitude": 44.43127318210377,
    "longitude": 26.07118959027369,
    "objectId": "-KTyI60jaUUuRBj-sIRR",
    "ownerId": "TlQZr5cqSIUboBO8k3bjz3q6oj63",
    "postText": "Bravo sefu' la Moose!",
    "replies": 4,
    "reportCount": 0,
    "score": 5,
    "updatedAt": "20161013134743"
}

这是我的代码

Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:authorization, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:api.joinmunch.com
Origin:http://localhost:8100
Referer:http://localhost:8100/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36

以下是响应标题

    let bodyString = JSON.stringify(user); // Stringify payload
    let headers      = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
    headers.append('Authorization', 'bearer MY_AUTH_TOKEN');

    let options       = new RequestOptions({ headers: headers }); // Create a request option
    options.withCredentials = true;

    return this.http.post(this.baseUrl + 'customer/register', bodyString, { headers: headers }) // ...using post request
                         .map((res:Response) => res.json()) // ...and calling .json() on the response to return data
                         .catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if an

screen shot of request and response

1 个答案:

答案 0 :(得分:0)

这只是CORS预检请求,而不是实际请求。您可以告诉我们将根据此

的实际请求设置标头
 (add-hook 'python-mode-hook
          (lambda ()
            (setq indent-tabs-mode t)
            (setq python-indent 4)
            (setq tab-width 4))
          (tabify (point-min) (point-max)))

预检请求询问服务器是否接受Access-Control-Request-Headers:authorization, content-type content-type标头,这是您设置的标头

如果您不了解CORS,请查看链接。也许它可以帮助您解决任何真正的问题,因为问题不在于未设置标头。