gpt4 book ai didi

api - Angular 2 token : Response for preflight has invalid HTTP status code 400

转载 作者:太空狗 更新时间:2023-10-29 17:18:06 24 4
gpt4 key购买 nike

我有一个在 Visual Studio Code 上运行的 Angular2/TypeScript 应用程序。

在 VS 2015 中运行的 API。这是 API 项目:http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

我可以使用 API 并创建新用户,但是当我尝试登录时(使用 Token 函数),我收到以下错误:XMLHttpRequest 无法加载 https://localhost:44305/Token .预检响应具有无效的 HTTP 状态代码 400

标题看起来像这样:

Request URL:https://localhost:44305/Token
Request Method:OPTIONS
Status Code:400
Remote Address:[::1]:44305
Response Headers
cache-control:no-cache
content-length:34
content-type:application/json;charset=UTF-8
date:Wed, 10 Aug 2016 19:12:57 GMT
expires:-1
pragma:no-cache
server:Microsoft-IIS/10.0
status:400
x-powered-by:ASP.NET
x-sourcefiles:=?UTF-8?B?QzpcQ2hlY2tvdXRcQVBJXzJ2czJcQVBJXEFQSVxUb2tlbg==?=
Request Headers
:authority:localhost:44305
:method:OPTIONS
:path:/Token
:scheme:https
accept:*/*
accept-encoding:gzip, deflate, sdch, br
accept-language:en-US,en;q=0.8,da;q=0.6,nb;q=0.4
access-control-request-headers:authorization
access-control-request-method:POST
cache-control:no-cache
origin:http://evil.com/
pragma:no-cache
referer:http://localhost:3000/signin
user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36

我的 Angular 服务如下所示:

 loginAccount(account: Account): Observable<string> {        
var obj = { Email: account.Email, Password: account.Password, grant_type: 'password' };
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions( {method: RequestMethod.Post, headers: headers });

let body = JSON.stringify(obj);
console.log('loginAccount with:' + body);

return this._http.post('https://localhost:44305/Token', body, options)
.map(this.extractData)
.catch(this.handleError);
}

当我使用 API 项目中的 AJAX 函数时:http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api然后它工作正常?我在 Angular POST 请求中做错了什么?

最佳答案

我找到了解决方案。感谢 API 站点上的评论:http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api

我必须为 application/x-www-form-urlencoded 设置正确的 header ; charset=UTF-8 并序列化我发布的对象。我找不到 Angular 序列化程序方法,所以我用 JavaScript 创建了自己的(从另一个 stackoverflow 站点复制)。

这是用户登录 API 并请求 token 时的最终调用,使用 Angular2 和 TypeScript 时:

 loginAccount(account: Account): Observable<string> {        
var obj = { UserName: account.Email, Password: account.Password, grant_type: 'password' };

let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
let options = new RequestOptions( {method: RequestMethod.Post, headers: headers });

let body = this.serializeObj(obj);

return this._http.post('https://localhost:44305/Token', body, options)
.map(this.extractData)
.catch(this.handleError);
}

private serializeObj(obj) {
var result = [];
for (var property in obj)
result.push(encodeURIComponent(property) + "=" + encodeURIComponent(obj[property]));

return result.join("&");
}

关于api - Angular 2 token : Response for preflight has invalid HTTP status code 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com