Asp.net web api 角度POST请求的标头为空
我正在开发一个Ionic 3移动应用程序,我对Angular的POST方法有问题 在登录页面中,我创建了一个表单,并尝试使用HTTP POST方法将数据发送到服务器。但在服务器.NETWebAPI中,我看到请求的头是空的 这是角边代码Asp.net web api 角度POST请求的标头为空,asp.net-web-api,ionic3,angular-http,Asp.net Web Api,Ionic3,Angular Http,我正在开发一个Ionic 3移动应用程序,我对Angular的POST方法有问题 在登录页面中,我创建了一个表单,并尝试使用HTTP POST方法将数据发送到服务器。但在服务器.NETWebAPI中,我看到请求的头是空的 这是角边代码 login(username, password):Observable<Object>{ let url : string = this.apiUrl+"/login"; let headers = new HttpHeader
login(username, password):Observable<Object>{
let url : string = this.apiUrl+"/login";
let headers = new HttpHeaders();
headers.append('Authorization', btoa(username+":"+password).toString());
return this.http.post(url,JSON.stringify({username,password}), {headers: headers});
}
下面是捕获请求的.NET端代码部分
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
var token = request.Headers.GetValues("Authorization").FirstOrDefault();
}
catch (Exception ex)
{
}
return base.SendAsync(request, cancellationToken);
}
通常,请求的url是localhost:8100,所以我认为服务器接受CORS
我该如何解决这个问题?在Web api中,您必须根据设置路线的方式来判断是post方法还是get方法
[EnableCors(origins: "http://localhost:8100", headers: "*", methods: "*")]
[HttpPost] // Decorate post this attribute in your controller
public Response Post()
{
return _mobileUserService.Login();
}
request = {Method: POST, RequestUri: 'http://localhost:41582/api/login', Version: 1.1, Content: System.Web.Http.WebHost.HttpControllerHandler+LazyStreamContent, Headers:
{
Connection: keep-alive
Accept: application/json
Accept: text/plain
Accept: */*
...
[EnableCors(origins: "http://localhost:8100", headers: "*", methods: "*")]
[HttpPost] // Decorate post this attribute in your controller
public Response Post()
{
return _mobileUserService.Login();
}