我正在尝试将 Angular js 2 服务中的 http post 调用的内容类型设置为 json。 但它没有设置为 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”。
最佳答案
您忘记导入 Headers
类:
import {Http, Headers} from 'angular2/http';
因此您没有看到任何错误,但 header 未包含在请求中...
关于angularjs - 无法将内容类型设置为 'application/json' Angular JS 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36474208/