2017-05-02 3 views
1

에 게시 된 데이터를하지 내가 보내고 데이터 : 내가 각도 2 서비스 코드에서 내 데이터를 게시각도 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

좋아 시원이 작동합니다 나는 jwt()을 청소 : 그것은 당신의 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; 
    }); 
} 

귀하의 서비스 아니에요 나는이 cpassword을 제거 주목 기능.

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