【问题标题】:Angular 2 http.post sending empty objectAngular 2 http.post 发送空对象
【发布时间】:2017-01-09 12:26:29
【问题描述】:

这是一个 authservice 登录,取自 from here

login(email: string, password: string): Observable<boolean> {
    return this.http.post(
        apiURL + '/admin/login',
        JSON.stringify({ email: email, password: password }))
        .map((response: Response) => {
            // login successful if there's a jwt token in the response
            let token = response.json() && response.json().token;
            if (token) {
                // set token property
                this.token = token;

                // store username and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify({ email: email, token: token }));

                // return true to indicate successful login
                return true;
            } else {
                // return false to indicate failed login
                return false;
            }
        });
}

在我的组件中,我尝试这样订阅:

login() {
    this.loading = true;
    console.log(this.model);
    this.authenticationService.login(this.model.email, this.model.password)
      .subscribe(result => {
        if(result === true) {
          this.router.navigate(['/']);
        } else {
          this.error = 'Username or password is incorrect';
          this.loading = false;
        }
      });
  }

我已经完成了控制台日志以确定数据是否实际被传递,是的。一切正常。

除了,发送的数据就是{ }

根据我的 Express 控制台,这是唯一通过 req.body 发送的内容。 req.headers表示内容类型正确,即Content-Type: application/json

我尝试使用 POSTMan 向 API 端点发送相同的 json 请求。工作正常。

Angular 2 的http.post 袖子还有什么技巧吗?

上面的代码有什么问题?

【问题讨论】:

  • 好的,数据是否通过?这里有点矛盾。一方面你说数据被传递,另一方面你说数据只是:{ } :)
  • @AJT_82 很抱歉造成混乱。我的意思是,一个空对象 { } 是 http.post 发送的唯一内容。
  • 什么是this.model
  • 我希望JSON.stringify({ email: email, password: password },) 对结尾的逗号不满意;您能否查看并确保这是minimal reproducible example
  • 我更倾向于关注this example

标签: angular express angular2-http


【解决方案1】:

在这里发布答案,可能会对某人有所帮助。感谢@cartant 的指点。我只需要仔细观察:

  • 我不需要明确设置标题。默认情况下,post 发送Content-Type: application/json
  • 没有必须对要发送的数据进行字符串化。

    login(email: string, password: string): Observable<boolean> {
    // this is optional
    // let headers = new Headers({ 'Content-Type': 'application/json' });
    // let options = new RequestOptions({ headers: headers });
    
    return this.http.post(
        apiURL + '/admin/login',
        { email: email, password: password },
        // options
        )
        .map((response: Response) => {
            // login successful if there's a jwt token in the response
            let token = response.json() && response.json().token;
            if (token) {
                // set token property
                this.token = token;
    
                // store username and jwt token in local storage to keep user logged in between page refreshes
                localStorage.setItem('currentUser', JSON.stringify({ email: email, token: token }));
    
                // return true to indicate successful login
                return true;
            } else {
                // return false to indicate failed login
                return false;
            }
        });
    }
    

【讨论】:

  • 那么,解决方案是什么?你忘记了什么?
  • @Enrico “我不必对要发送的数据进行字符串化。”
猜你喜欢
  • 2017-03-08
  • 2017-05-12
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 2017-07-05
  • 2017-06-15
  • 1970-01-01
相关资源
最近更新 更多