【问题标题】:can not set content type to 'application/json' angular js 2 [duplicate]无法将内容类型设置为 'application/json' angular js 2 [重复]
【发布时间】:2016-04-07 11:00:06
【问题描述】:

我正在尝试将内容类型设置为 json,以便在 angular js 2 服务中进行 http post 调用。 但它没有设置为 json 导致在 api 参数中获取空值

import {Injectable} from 'angular2/core';
import {Http} from 'angular2/http';
@Injectable()
export class DeviceService {
public headers: Headers;

constructor(private http:Http) {
    this.headers = new Headers();
    this.headers.append('Content-Type', 'application/json');
}

/*getAllUsers service to get data(users) from file or database*/
getDevice() {
    return this.http.get('assets/api/device.json').map(
        response => response.json()
    );
}

save(model) 
{
    return this.http.post('http://localhost:14/someapi/method',
        JSON.stringify(model),{headers: this.headers})
        .map(response => response.json());
}

但是在进行 post 调用时,它总是在 firebug 控制台中将内容类型显示为 'text/plain;charset=UTF-8'。

【问题讨论】:

标签: angularjs angular http-post


【解决方案1】:

您忘记导入 Headers 类:

import {Http, Headers} from 'angular2/http';

所以您看不到任何错误,但请求中不包含标头...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 1970-01-01
    • 2021-01-04
    • 1970-01-01
    • 2015-10-09
    • 2023-03-06
    • 2013-05-02
    相关资源
    最近更新 更多