Passing client data from form to server

zolf
zolf used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jim RiddlesPrepress/OMS Specialist

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
Commented:
There are a few functions you can use to typecast a variable:
Number();
parseInt();
parseFoat();

For your purposes, parseInt() seems to be the best choice.  Have a look here for more detailed information.

Author

Commented:
cheers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial