我要在发布用户数据时获取令牌代码。但是,服务器对我的响应为400错误请求。
POST http://xxx/api/auth 400 (Bad Request)
我可以通过邮递员获得令牌代码
我的AuthService
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { Token } from './token';
@Injectable({
providedIn: 'root'
})
export class AuthService {
api = 'http://xxx/api/auth';
token;
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
}
constructor(
private http: HttpClient,
private router: Router
) { }
login(user):Observable<Token>{
this.http.post<Token>(this.api, JSON.stringify(user), this.httpOptions).subscribe(
res => {
localStorage.setItem('token', res.token)
},
err => console.log(err)
)
}
}
我的登录组件
import { Component, OnInit } from '@angular/core';
import { AuthService } from '../services/auth.service';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
loginUserData = {}
constructor(
private auth: AuthService
) { }
ngOnInit() {
}
Login(){
this.auth.login(this.loginUserData);
}
}
请帮助我我所缺少的。我试图以stringfy格式传递数据,但这也给了我同样的错误。