2016-06-25 20 views
0

現在、RESTfulをバックエンドとして使用しているAngular 2プロジェクトで作業しています。角2 HTTPポスト:応答オブジェクトを取得できません。

ここで私の質問に役立つ短いシーンアリーナです。

ユーザーがログインすると、いくつかのユーザーの詳細を表示するプロファイル/注文ページにリダイレクトします。

これを達成するために、私はRegister.ts を持っています。ここに私のコードはこのように見えます。私は、ユーザーの詳細情報を表示することができる午前

onLogin(_username:string, _password:string) 
    { 
     const data = { 
      username:_username, 
      password:_password, 
      } 

     this._registerService.Login(data) 
      .subscribe(
       data=> resolve(data.json()) 
      ) 
     console.log(this.response);} 
onPost(username:string, email:string, password:string, housenumber:string, street:string, city:string) 
{ 
    const data = { 
     username:username, 
     email:email, 
     password:password, 
     housenumber:housenumber, 
     street:street, 
     city:city 
    } 
    if (data == null) 
     this.isValid == false; 

    else { 
     this.isValid == true; 
     this._registerService.Register(data).subscribe(
      data=> this.response = JSON.stringify(data), 
      error => console.log(error)), 
      data=>console.log(data); 
      localStorage.setItem('user', JSON.stringify(data) 
      ); 

     if(localStorage.getItem("user") != null) { 
      this._registerService.isLoggedIn = true; 
      this._router.navigate(['/order']); 
     } 
     else 
     //replace this with a page 
      alert("Sign in with a correct name!"); 

    } 
} 

以下のように彼らは、申し込み時に

export class RegisterService{ 
    public isLoggedIn:boolean; 
    public userDetails:any; 
    public newUser = new User("","","",null); 

    constructor(private _http: Http){ 
     this.isLoggedIn = false; 
    } 


    Register(data:any) { 
     const body = JSON.stringify(data); 
     // console.log(data); 
     this.userDetails = data; 
     // console.log("userdets: "+this.userDetails.username); 
     const headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 
     return this._http.post(`http://192.168.20.17:8080/webshop/api/user/create?username=`+data.username+`&password=` 
           +data.password+`&email=`+data.email+`&city=`+data.city+`&housenumber=`+data.housenumber+ 
           `&street=`+data.street, body, {headers: headers} 
     ); 
    } 
Login(data:any):Observable<any>{ 
     const body = JSON.stringify(data); 
     // console.log(data); 
     const headers = new Headers(); 
     headers.append('Content-Type', 'application/json'); 
     return this._http.post(`http://192.168.20.17:8080/webshop/api/user/login?username=` 
      +data.username+`&password=`+data.password, body,{headers:headers}) 
      .map((res:Response)=> JSON.stringify(res)); 
     // console.log(this.newUser); 

    } 

そして、私のサインin.component.tsに、私はこのサービスを利用する方法がありますが、とき私はこのようなJSONオブジェクトであることを意味する応答を得ることができない、ユーザがログインすると:

{ 
    "address": { 
    "addition": null, 
    "city": "Nijmegen", 
    "housenumber": "908", 
    "street": "de Voorstenkamp" 
    }, 
    "email": "[email protected]", 
    "password": "678", 
    "username": "feedme" 
} 

を私はUser.ts

、2つのクラスを作成しようとしました
import {Address} from "./Address.ts"; 
export class User { 
    username:string; 
    email:string; 
    password:string; 
    address:Address; 

    constructor(username:string, email:string, password:string,address:Address){ 
     this.username = username; 
     this.email = email; 
     this.password = password; 
     this.address = address; 
    } 

    public static createEmptyUser():User{ 
     return new User("","","",null); 
    } 
} 

そして、この2つのクラスを使用してaddress.tsは

export class Address { 
    street:string; 
    housenumber:string; 
    addition:string; 
    city:string; 


    constructor(street:string, housenumber:string,addition:string,city:string) { 
     this.street = street; 
     this.housenumber = housenumber; 
     this.addition = addition; 
     this.city = city; 
    } 

    public static createEmptyAddress():Address { 
     return new Address("","","",""); 
    } 

} 

、私は、ユーザーオブジェクトを取得するには応答を解析しようとしたが、私はまったく応答を取得することはできませんよ。

誰でも私を助けることができますか?私は本当にそれを感謝します。

答えて

0

こんにちは、私はちょうど任意のHTTPレスポンスから身体の一部を取得する方法の例を示しています:私はteamService.tsでHTTP POSTリクエストと呼ばれている上記のコードで

getMemberList(){ 
     this.teamService.getMemberList().subscribe(
     (res)=>{ 
        console.log("getMemberList: ",res.json()); 
     }, 
     (err)=>{ 
        console.log(err); 
     } 
    ) 
    } 

。したがって、APIから応答が来たら、res.jsonを実行して、応答から身体部分を取得する必要があります。

関連する問題