2017-09-07 36 views
1

我使用角度爲2和離子的用戶名和密碼在我的應用程序上對用戶進行身份驗證。 當我使用subscribe方法我得到的數據從服務器返回這樣發佈返回帶有斜線的json數據

{"_body":"[{\"FullName\」:\」user\」,\」PhoneNo\」:\」0001\」}]」,」status":200,"ok":true,"statusText":"OK","headers":{"content-type":["text/html"]},"type":2,"url":"http://webserver.com/login.php"} 

與斜線的數據。

當我使用rxjx方法未能從服務器得到任何迴應,它讀取error sending 如何解決我的劇本,所以我得到的只是像普通的JSON

這樣

[{"FullName」:」user」,」PhoneNo」:」0001」}] 

數據返回

下面是我的腳本,我用它來發布到服務器

this.http.post("http://webserver.com/login.php",{'username':this.username,'password':this.password}).subscribe(data=>{ 
console.log(JSON.stringify(data)); 
},error=>{ 
    console.log("error sending") 
}) 

回答

2

當獲得第r從服務器響應,您需要提取該響應的正文。你可以通過添加這樣的代碼.map(res => res.json())這樣做:

let data = { 'username': this.username, 'password': this.password }; 

this.http.post("http://webserver.com/login.php", data) 
    .map(res => res.json()) // <---- Here! 
    .subscribe(
     data => { 
      console.log(JSON.stringify(data)); 
     }, 
     error => { 
      console.log("error sending") 
     });