Passing client data from form to server

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

zolfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim RiddlesPrepress/OMS SpecialistCommented:
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.
0
zolfAuthor Commented:
Thanks for your comment. How can i change this on the client side instead of the server side. any suggestions!!
0
Jim RiddlesPrepress/OMS SpecialistCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zolfAuthor Commented:
cheers
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSON

From novice to tech pro — start learning today.