【发布时间】:2018-04-15 09:38:37
【问题描述】:
我正在使用以下代码发送一个帖子请求
import { Http, Headers, Response } from '@angular/http';
@Injectable()
export class AuthenticationService {
private _options = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http) { }
login(username: string, password: string) {
return this.http.post('http://localhost:56451/map',
{ "username": username, "password": password },
this._options);
}
}
但是我在 vscode 中遇到以下错误
Argument of type '{ headers: HttpHeaders; }' is not assignable to parameter of type 'RequestOptionsArgs'. Types
of property 'headers' are incompatible. Type 'HttpHeaders' is not assignable to type 'Headers'. Property 'forEach'
is missing in type 'HttpHeaders'.
请帮助澄清相关概念
【问题讨论】:
-
如果您使用 angular 4
new Headers({ 'Content-Type': 'application/json' });,请使用来自 @angular/http 的标头而不是 HttpHeaders -
那是另一个问题,我在 SO 上看到过几个关于这个的问题,搜索它你肯定会得到答案!
-
我想你会得到 403,因为你在 POST 请求中提供标头,所以 Angular 首先发送一个 OPTIONS 请求,你的服务器端逻辑没有设置。您需要在服务器上启用 CORS,否则您将获得 403/405。
-
请根据上述 cmets 的调查结果更新您的问题
-
this.options 定义在哪里?
标签: javascript angular http post http-headers