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

后端 未结 4 2049
予麋鹿
予麋鹿 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:26

    I was also facing same issue from last week and searched on google and stack overflow but all solutions in vein. but after lot of reading and investigation we have found below solution, we were facing issue in only POST method,GET called successfully.

    Instead of directly passing Options we need to first stringify option object like JSON.stringify(options)

    CreateUser(user:IUser): Observable {
            let headers = new Headers();
            headers.append('Content-Type', 'application/json');
            headers.append('Accept', 'application/json');
            let options = new RequestOptions({ headers: headers });
            return this._http.post('http://localhost:22736/api/Employee/Create', **JSON.stringify(options)**)
                .map((res: Response) => {
                    return res.json();
                })
                .catch(this.handleError);
        }
    

    It worked for me, Hope it will help others too.

提交回复
热议问题