angular - Ionic2: Send Submit form data to service -
i using ionic2, want send data form web service using post request, every time print form data found empty , service send me not received data. html code:
<div class="login-box"> <form #registerform="ngform"> <ion-row> <ion-col id="imagecontainer"> <button (click)="upload()" ion-button>take pic & upload </button> </ion-col> </ion-row> <ion-row> <ion-col> <ion-list inset> <ion-item> <ion-label floating>{{ 'username' | translate }}</ion-label> <ion-input type="text" name="username" #username required></ion-input> </ion-item> <ion-item> <ion-label floating>{{ 'mobilenumber' | translate }}</ion-label> <ion-input type="text" name="mobile" #mobile required></ion-input> </ion-item> <ion-item> <ion-label floating>{{ 'emailaddress' | translate }}</ion-label> <ion-input type="email" name="email" #email required></ion-input> </ion-item> <ion-item> <ion-label floating>{{ 'password' | translate }}</ion-label> <ion-input type="password" name="password" #password required></ion-input> </ion-item> <ion-item> <ion-label floating>{{ 'confirmpassword' | translate }}</ion-label> <ion-input type="password" name="confirmpass" #confirmpass required></ion-input> </ion-item> </ion-list> </ion-col> </ion-row> <ion-row> <ion-col class="signup-col"> <button ion-button class="submit-btn" (click)="signup(username.value,email.value,password.value,mobile.value,confirmpass.value)" full type="submit" [disabled]="!registerform.form.valid">sign up</button> <button ion-button class="register-btn" block clear (click)="login()">sign in</button> </ion-col> </ion-row> </form> </div>
the service method:
register(username,email,password,mobile,) { let body = new formdata(); body.append('user_name',username); body.append('email',email); body.append('mobile',mobile); body.append('password',password); body.append('image','dsdsdsddsd'); console.log('body service: ',body); let headers = new headers(); headers.append("content-type","application/formdata"); return this.http.post(this.registerurl,body,{headers:headers}) .map((response:response)=>response.json()) }
the js:
signup(username,email,password,mobile,confirmpass) { this.userservice.register(username,email,password,mobile) .subscribe(data=>{ console.log('register data service: ',data); }, (err)=> console.log('error: ',err) ) }
this received data service: object {result: array(0), status: object} result : array(0) status : object message : array(4) 0 : "the user name field required." 1 : "the email field required." 2 : "the mobile field required." 3 : "the password field required." length : 4
you have stringify data before send request use function
let body = json.stringify(body) return this.http.post(this.registerurl,body,{headers:headers}) .map((response:response)=>response.json())
Comments
Post a Comment