2017-05-02 51 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; 
      }); 
    } 

我張貼從角2服務代碼我的數據如下:

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

你能分享你如何調用創建方法與價值的代碼片段? – Born2Code

+0

你可以嘗試添加標題的請求選項中的正文對象並嘗試? – Born2Code

0

我認爲這個問題是你的頭建設。嘗試改變這一點:

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

這樣:

'Content-Type': 'application/json' 

UserRegisterModel還引入領域cpassword

0

好涼,這應該工作:

您最初的頁面應該是這樣的:請注意,我刪除了cpassword,因爲它在你的API模型不是

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; 
    }); 
} 

您的服務:我清理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; 
}