We help IT Professionals succeed at work.

Passing client data from form to server

221 Views
Last Modified: 2018-03-21
Hello there,

I have a HTML5 form with Angular 2 and I have a form that takes string and integer value and sends it to my Java backend server. But for some reason my integer value is being sent as string in the JSON which passes to the server. Below is the code. I will appreciate your help!

cheers
Zolf

<div class="form-group row">
                <label class="col-form-label col-md-4" for="fax">Fax <span class="required">*</span></label>
                  <div class="col-md-7">
                    <input class="form-control" 
                                    id="fax" 
                                    type="text"  
                                    formControlName="fax" />
                  </div>
              </div>

Open in new window


this.companyForm.patchValue({
            name_en: this.companyProfile.name_en,
            address: this.companyProfile.address,
            fax: this.companyProfile.fax,
            email: this.companyProfile.email
        });

Open in new window


saveCompanyProfile(): void {
        if (this.companyForm.dirty && this.companyForm.valid) {
            // Copy the form values over the CompanyProfile object values
            console.log("  ZZZZZZZZ    "+JSON.stringify(this.companyForm.value));
            let p = Object.assign({}, this.companyProfile, this.companyForm.value);

            this.companyService.saveCompanyProfile(p)
                .subscribe(
                    () => this.onCompanyProfileComplete(),
                    (error: any) => this.errorMessage = <any>error
                );
        } else if (!this.companyForm.dirty) {
            this.onCompanyProfileComplete();
        }
    }

Open in new window


and my Service class that talks to the server looks like so,

private updateCompanyProfile(companyprofile: ICompanyProfile, options: RequestOptions): Observable<ICompanyProfile> {
        const url = `${this.referenceUrl}/${companyprofile.name_en}`;
        console.log('url: ' + url)
        console.log('updateCompanyProfile: ' + JSON.stringify(companyprofile))
        return this.http.put(url, companyprofile, options)
            .map(() => companyprofile)
            .do(data => console.log('update companyprofile: ' + JSON.stringify(data)))
            .catch(this.handleError);
    }

Open in new window

Comment
Watch Question

Jim RiddlesPrepress/OMS Specialist
CERTIFIED EXPERT

Commented:
That would seem to be expected behavior, to me.  My thought is you should convert the value to an integer using Integer.parseInt(), in your Java backend code.

Author

Commented:
Thanks for your comment. How can i change this on the client side instead of the server side. any suggestions!!
Prepress/OMS Specialist
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
cheers