简体   繁体   English

发送到ASP.NET Web API时,请求标头中Angular 5缺少Content-type

[英]Angular 5 missing Content-type in request headers when send to ASP.NET Web API

Currently, I'm using Angular 5 to build a website. 目前,我正在使用Angular 5建立网站。 My website using 0Auth 2.0 to secure a request send to ASP.net Web API 2.0. 我的网站使用0Auth 2.0来保护发送到ASP.net Web API 2.0的请求的安全。 So, when i try sending a request to get an access token. 因此,当我尝试发送获取访问令牌的请求时。 I use this function : 我使用这个功能:

userAuthentication(userName, password) {
let data = "username=" + userName + "&password=" + password + "&grant_type=password&client_id=JCWebApp";
return this.http.post(this.rootUrl + '/login', data,{headers:'Content-Type':'application/x-www-form-urlencoded'}});

} }

But web api server return for me response with status code 400. When I check an request header of login request, it like this : 但是Web api服务器为我返回状态码为400的响应。当我检查登录请求的请求标头时,它像这样:

Accept 接受
text/html,application/xhtml+xm…plication/xml;q=0.9, / ;q=0.8 Accept-Encoding gzip, deflate Accept-Language en-US,en;q=0.5 Access-Control-Request-Headers text / html,application / xhtml + xm…plication / xml; q = 0.9, / ; q = 0.8 Accept-Encoding gzip,deflate Accept-Language zh-cn,en; q = 0.5 Access-Control-Request-Headers
no-auth Access-Control-Request-Method 无认证访问控制请求方法
POST Connection 开机自检连接
keep-alive Host 保持活动主机
localhost:9810 Origin 本地主机:9810来源
http://localhost:4200 User-Agent http:// localhost:4200用户代理
Mozilla/5.0 (Windows NT 10.0; …) Gecko/20100101 Firefox/59.0 Mozilla / 5.0(Windows NT 10.0;…)Gecko / 20100101 Firefox / 59.0

I researched on the internet, and try many way, all most the answer same as : - Angular HttpClient doesn't send header . 我在互联网上进行了研究,并尝试了多种方法,所有的答案都与以下内容相同: -Angular HttpClient不发送header But it not working with my situation. 但这不适合我的情况。

So please help me explain why and how to resolve this case. 因此,请帮助我解释为什么以及如何解决此案件。

Thanks. 谢谢。

you need to enable the CORS in your Web API 您需要在Web API中启用CORS

download this in nuget Microsoft.Owin.Cors 在nuget Microsoft.Owin.Cors中下载此文件

and in ConfigureAuth(IAppBuilder app) located in your Web API 并在您的Web API中的ConfigureAuth(IAppBuilder app)

add this line of code app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 添加此行代码app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM