我试图将凭据从Angular传递到C#,但是Angular收到404。我正在遵循教程from here。我如何传递数据有问题吗?我确定路线是正确的。
选项http://localhost:52008/Account/GetJWT 404(未找到)
登录:1无法加载http://localhost:52008/Account/GetJWT:飞行前响应没有HTTP正常状态。
从AccountController中获取:
[Route("[controller]/[action]")]
public class AccountController : Controller
{
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> GetJWT([FromBody]LoginModel user)
{
myJWT m = new myJWT(user);
return Ok(new { Token = m.token });
}
}
正在调用上面的Controller的角度代码:
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Component } from '@angular/core';
import { Router } from "@angular/router";
import { NgForm } from '@angular/forms';
@Component({
selector: 'login',
templateUrl: './login.component.html'
})
export class LoginComponent {
invalidLogin: boolean;
constructor(private router: Router, private http: HttpClient) { }
login(form: NgForm) {
let credentials = JSON.stringify(form.value);
console.log(credentials);
this.http.post("http://localhost:52008/Account/GetJWT", credentials, {
headers: new HttpHeaders({
"Content-Type": "application/json"
})
}).subscribe(response => {
let token = (<any>response).token;
localStorage.setItem("jwt", token);
this.invalidLogin = false;
this.router.navigate(["/"]);
}, err => {
this.invalidLogin = true;
});
}
}