Angular2 Http发布400

时间:2017-02-10 14:39:40

标签: json angular

我有这项服务:

@Injectable()
export class HttpserviceService {
  baseUrl: string = "http://localhost:3000/";
  headers = new Headers({
    'accept': 'application/json'
  });

 post(url: string,  data: any): Observable<any> {

    //send post request
    return this.http.post(this.baseUrl+url, JSON.stringify(data))
      .map(this.extractData) //this works
      .catch(this.handleError); //this as well
  }
}

当我订阅该方法时:

user: User = {
  username: "test",
  password: "12345"
}
authUrl: string = 'user/auth';
return this.http.post(this.authUrl, user)
  .subscribe(data => {
    //console.log(data);
  });

我得到了

  

状态代码:400错误请求

有什么不对?

当我请求使用postman时,一切正常

enter image description here

1 个答案:

答案 0 :(得分:3)

在邮递员中,我看到你有1个标题。你没有发送角度

尝试以下内容,标题应作为第三个参数

post(url: string,  data: any): Observable<any> {

    //send post request
    return this.http.post(this.baseUrl+url, JSON.stringify(data),  {headers: this.headers})
      .map(this.extractData) //this works
      .catch(this.handleError); //this as well
  }