我有一个 Angular 2 登录表单,我正在尝试通过 API 调用调用 JWT 令牌控制器。
我的 user.service.ts 如下:-
login(username, password) {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let credentials = JSON.stringify({ username, password });
console.log(username, password);
return this._http.post('http://localhost:5000/api/jwt', credentials, { headers: headers })
.map(res => res.json())
.map((res) => {
if (res.success) {
localStorage.setItem('auth_token', res.auth_token);
this.loggedIn = true;
}
return res.success;
});
}
当我执行 console.log(username, password); 我在表单中输入了正确的用户名和密码。
然后我有如下控制器: -
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Get(string username, string password)
{
//code here
}
我的问题是用户名和密码始终为空。
我曾尝试使用 Postman使用用户名和密码向“ http://localhost:5000/api/jwt ”发帖,并且效果很好。
如何将用户名和密码从 Angular 2 表单传递给控制器?
感谢您的帮助和时间
约翰