Angular 2、Http 和 Not 设置 Content-Type "application/json"
Angular 2, Http and Not setting Content-Type "application/json"
我正在尝试在服务中使用 Angular 2(2.0.0-rc.1) 执行简单的检查登录方法。当我尝试将 content-type 设置为 application/json 时,它在发送到我的网络 api 后端时从不设置 headers。
import { Injectable } from '@angular/core';
import { HTTP_PROVIDERS, Http, Response, Headers, RequestOptionsArgs, RequestMethod, Request, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Other Modules Left out for Security } from 'Hidden';
@Injectable()
export class LoginService {
private _http: Http;
constructor(http: Http) {
this._http = http;
}
CheckLogin(model: CredentialModel) {
let url = 'http://localhost:51671/api/Login';
let data = JSON.stringify(model);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
let requestOptions = new RequestOptions({
method: RequestMethod.Post,
url: url,
headers: headers,
body: data
});
console.log(requestOptions);
console.log(data);
return this._http.post(url, data, requestOptions);
}
}
来自网络的请求Api
OPTIONS /api/Login HTTP/1.1
Host: localhost:51671
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://localhost:5616
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2754.0 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
DNT: 1
Referer: http://localhost:5616/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
使用 PostMan 使用完全相同的数据效果很好! Angular 2 使用 POST 方法发送 application/json 似乎有问题。
解决此问题的任何帮助都会有所帮助。愿意尝试任何建议。
Http
class 的 post
方法接受类型为 RequestOptionsArgs
而不是 RequestOptions
的对象。
class RequestOptionsArgs {
url : string
method : string | RequestMethod
search : string | URLSearchParams
headers : Headers
body : any
withCredentials : boolean
}
你可以在第三个参数中按字面意思这样指定:
return this._http.post(url, data, { headers: headers });
RequestOptionsArgs
是Angular2中的一个接口,所以你可以指定RequestOptions
作为一个值。
在 Angular 4 和更高版本中设置 Headers 现在应该这样设置:
让 headers = 新的 HttpHeaders({
"content-type": "application/json",
"Accept": "application/json"
});
this.http.post("apiUrl", 数据, {headers: this.headers})
我正在尝试在服务中使用 Angular 2(2.0.0-rc.1) 执行简单的检查登录方法。当我尝试将 content-type 设置为 application/json 时,它在发送到我的网络 api 后端时从不设置 headers。
import { Injectable } from '@angular/core';
import { HTTP_PROVIDERS, Http, Response, Headers, RequestOptionsArgs, RequestMethod, Request, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Other Modules Left out for Security } from 'Hidden';
@Injectable()
export class LoginService {
private _http: Http;
constructor(http: Http) {
this._http = http;
}
CheckLogin(model: CredentialModel) {
let url = 'http://localhost:51671/api/Login';
let data = JSON.stringify(model);
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('Accept', 'application/json');
let requestOptions = new RequestOptions({
method: RequestMethod.Post,
url: url,
headers: headers,
body: data
});
console.log(requestOptions);
console.log(data);
return this._http.post(url, data, requestOptions);
}
}
来自网络的请求Api
OPTIONS /api/Login HTTP/1.1
Host: localhost:51671
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: POST
Origin: http://localhost:5616
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2754.0 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
DNT: 1
Referer: http://localhost:5616/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
使用 PostMan 使用完全相同的数据效果很好! Angular 2 使用 POST 方法发送 application/json 似乎有问题。
解决此问题的任何帮助都会有所帮助。愿意尝试任何建议。
Http
class 的 post
方法接受类型为 RequestOptionsArgs
而不是 RequestOptions
的对象。
class RequestOptionsArgs {
url : string
method : string | RequestMethod
search : string | URLSearchParams
headers : Headers
body : any
withCredentials : boolean
}
你可以在第三个参数中按字面意思这样指定:
return this._http.post(url, data, { headers: headers });
RequestOptionsArgs
是Angular2中的一个接口,所以你可以指定RequestOptions
作为一个值。
在 Angular 4 和更高版本中设置 Headers 现在应该这样设置:
让 headers = 新的 HttpHeaders({ "content-type": "application/json", "Accept": "application/json" });
this.http.post("apiUrl", 数据, {headers: this.headers})