Ionic 2 http post请求删除默认标头

时间:2017-12-07 22:53:28

标签: ionic-framework ionic2

我是Ionic 2的新手。我向Spring Rest-api发出了一个http post请求。作为标头,我需要设置'content-type': 'application/json',但Ionic 2会自动将content-type设置为application/json; charset=UTF-8,这会导致我的REST服务以415状态响应。

这是我的代码:

register(user: User){
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');

    return this.http.post(apiUrl + 'users', user, {headers:headers})
        .then((response: Response) => {
            console.log(response);
            console.log(JSON.stringify(user, null , 2));
            console.log(JSON.stringify(headers, null, 2));
        });
}

以下是我在发布帖子请求时收到的错误:

[00:46:35]  error opening ws message: {"category":"console","type":"log","data":["error:
        ",{"status":415,"url":"**MY_URL**","headers":{"x-android-received-millis":"1512686795738","x-frame-options":"DENY","pragma":"no-cache","content-type":"application/json;charset=UTF-8","x-content-type-options":"nosniff","date":"Thu,
        07 Dec 2017 22:46:35 GMT","strict-transport-security":"max-age=31536000 ; includeSubDomains","via":"1.1
        vegur","connection":"keep-alive","x-android-sent-millis":"1512686795652","cache-control":"no-cache,
        no-store, max-age=0,
        must-revalidate","transfer-encoding":"chunked","server":"Cowboy","x-android-response-source":"NETWORK
        415","x-android-selected-protocol":"http/1.1","expires":"0","x-xss-protection":"1;
        mode=block"},"error":"{\"timestamp\":1512686795600,\"status\":415,\"error\":\"Unsupported Media
        Type\",\"exception\":\"org.springframework.web.HttpMediaTypeNotSupportedException\",\"message\":\"Content
        type 'application/x-www-form-urlencoded;charset=UTF-8' not supported\",\"path\":\"/api/users\"}"}]}

请在错误日志中检查标题对象。我怎样才能只留下'content-type': 'application/json'

非常感谢!

0 个答案:

没有答案