无法将内容类型设置为 'application/json' angular js 2
can not set content type to 'application/json' angular js 2
我正在尝试将我在 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
class:
import {Http, Headers} from 'angular2/http';
因此您没有看到任何错误,但 headers 未包含在请求中...
我正在尝试将我在 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
class:
import {Http, Headers} from 'angular2/http';
因此您没有看到任何错误,但 headers 未包含在请求中...