0

我正在为我的应用程序使用 angular 4.2.6。我有这样的服务

checkStaff(email: any) {

  return this._http.post(this.url + "/Impsapi/getStaff", JSON.stringify(email)).map(
    (resp) => resp
  )

}

checkStaff(email:any){ return this._http.post(this.url+"/Impsapi/getStaff",JSON.stringify(email)).map( (resp)=> resp )

}

this.loginServ.checkStaff(this.user)
  .subscribe(
    userData => {
      this._return = userData;
      console.log(this._return);
    }
  );

服务器返回 JSON 作为响应。但是当我记录输出时,我得到以下 记录的响应

请我需要使用响应正文中的数据。我无法将 ._body 转换为正确的 json 并用于应用程序。请帮忙

4

3 回答 3

0

响应数据为JSON字符串形式。应用程序必须通过调用将该字符串解析为 JavaScript 对象res.json()

return this._http.post(this.url + "/Impsapi/getStaff", JSON.stringify(email)).map(
    (resp) => resp.json()
  )

更新 尝试以下代码片段

checkStaff(email: any) {
    return this._http.post(this.url + "/Impsapi/getStaff", JSON.stringify(email))
    .map(res => {return res.json()})


  }
于 2018-01-05T12:59:57.057 回答
0

试试这个:

this.loginServ.checkStaff(this.user)
  .subscribe(
    userData => {
      this._return = userData.json();
      console.log(this._return);
    }
  );

我的意思是你的checkStaff:

checkStaff(email: any): Observable<Response> {

  return this._http.post(this.url + "/Impsapi/getStaff", JSON.stringify(email));

}


export classMyResp
{
id: string;
 /*so on...*/
}

这将为您提供响应主体(如果有)。

于 2018-01-05T14:08:44.227 回答
0

我的问题解决了。我的 PHP 托管在 wampserver 上。在某种程度上,当我调用服务器时,总是返回无效的 JSON。我不得不使用该ob_clean()功能,一切都很好。

于 2018-01-15T11:07:15.827 回答