2017-05-02 7 views
1

に掲載のデータを取得できません:私は角2サービスコードから自分のデータを掲載- ウェブAPI:私がデータを送信していますページからのAPI

 register() { 
     this.loading = true; 
     this.Register = { firstName: 'aa', lastName: 'aa', email: 'aa', password: 'aa', cpassword: 'aa', gender: "male", dob: '2017-05-02' }; 
     this.userService.create(this.Register) 
      .subscribe(
      data => { 
       alert("aa"); 
      }, 
      error => { 
       // this.alertService.error(error); 
       this.loading = false; 
      }); 
    } 

は以下の通りです:

create(UserRegister: UserRegister) { 
     return this.http.post('http://localhost:53625/api/UserLoginAPI/InsertUser', UserRegister, this.jwt()).map((response: Response) => response.json()); 
    } 

    private jwt() { 
      let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }); 
      return new RequestOptions({ headers: headers }); 
    } 
私は、APIでコードを書くこのため

は以下の通りです:

 [HttpPost] 
     public IHttpActionResult InsertUser(UserRegisterModel UserRegister) 
     { 
      int returnval = 0; 
      returnval = objUserLoginBAL.InsertUser(objUserRegisterModel); 
      return Json(returnval); 
     } 

私は、API側でコールを取得しますが、私のオブジェクトは、任意の値を取得できません。それは、画像に示されている:

enter image description here

答えて

0

は、以下試してみてください。

create(UserRegister: UserRegister) { 
    return this.http.post('http://localhost:53625/api/UserLoginAPI/InsertUser', UserRegister, this.jwt(UserRegister)).map((response: Response) => response.json()); 
} 

private jwt(UserRegister: UserRegister) { 
let _body = JSON.stringify(UserRegister); 
let headers = new Headers({ 'Content-Type': 'application/json;charset=utf-8' }); 

return new RequestOptions({ 
      headers: headers, 
      body: _body }); 
} 
+0

はい私はオブジェクトを初期化します –

+0

createメソッドをどのように値で呼び出すかについてコードスニペットを共有できますか? – Born2Code

+0

ヘッダーのリクエストオプションでbodyオブジェクトを追加してみてみることはできますか? – Born2Code

0

私は問題があなたのヘッダの構造だと思います。これに

'Content-Type': 'application/x-www-form-urlencoded' 

:この変更してみ

'Content-Type': 'application/json' 

をまたUserRegisterModelcpasswordためのフィールドを紹介します。

0

[OK]をクールな、これは動作するはずです:

をあなたの最初のページには、次のようになります。それは

register() { 
    this.loading = true; 

    this.Register = { 
    dob: '2017-05-02' 
    email: 'aa', 
    firstName: 'aa', 
    gender: "male", 
    lastName: 'aa', 
    password: 'aa', 
    }; 

    this.userService.create(this.Register) 
    .subscribe((data:any) => { 
     alert("aa"); 
    },(error:any) => { 
     // this.alertService.error(error); 
     this.loading = false; 
    }); 
} 

あなたのサービス、あなたのAPIモデルではないように注意してください、私はcpasswordを削除:私はjwt()をクリーンアップ関数。

import { Http, Response, Headers, RequestOptionsArgs} from '@angular/http'; 

create(userRegister: UserRegister) { 
    let url:string = 'http://localhost:53625/api/UserLoginAPI/InsertUser'; 

    return this.http.post(url, userRegister, this.jwt()) 
    .map((res:Response) => res.json()); 
} 

private jwt():RequestOptionsArgs { 
    let headers:Headers = new headers(); 

    headers.append('Content-Type', 'application/json'); 

    let options:RequestOptionsArgs = new RequestOptions(); 

    options.headers = headers; 

    return options; 
} 
関連する問題