Angular 2 Token: Response for preflight has invalid HTTP status code 400

后端 未结 4 2060
予麋鹿
予麋鹿 2021-02-09 10:45

I have an Angular2/TypeScript application running i Visual Studio Code.

An API running in VS 2015. This is the API project: http://www.asp.net/web-api/overview/security/

4条回答
  •  旧巷少年郎
    2021-02-09 11:38

    Another native solution is by using HttpParams class and it's toString() method:

     let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
        let options = { headers, observe: 'response' };
    
       const body = new HttpParams()
          .set('grant_type', 'password')
          .set('username', accountInfo.username)
          .set('password', accountInfo.password);
    
        return this._http.post('https://localhost:44305/Token',  body.toString(), options)
    

    toString() - Serialize the body to an encoded string, where key-value pairs (separated by =) are separated by &s.

    Note. Also it works without setting the headers

提交回复
热议问题