send integers in FormData
Asked Answered
I

4

13

When I send an integer to the backend , I receive it as a string , I can't figure out why ?

var formdata = new FormData();

for (var i = 0; i < scope.user.values.length; i++) {
    formdata.append('values[]', scope.user.values[i]);
}

I receive values as string while they should be in integers

Injunction answered 23/11, 2015 at 12:11 Comment(1)
A
17

You actually cannot send integers, everything will be in string format and in key-value pairs when you use formData. Alternatively you can convert them to required datatype in backend.

Know more about FormData here

Adkins answered 23/11, 2015 at 12:15 Comment(1)
What a silly feature...Botanomancy
L
5

All values sent by HTML to the server-side are received as strings. You have to convert that data into integers in the backside, which shouldn’t be too difficult. If you specify which language your server-side is using maybe we could let you know how to convert string into integers in it.=, or ever better, you can look it up.

Luby answered 23/11, 2015 at 12:25 Comment(2)
Thank You , do you Advice using formdata or there is a better solution ?Injunction
It depends on the data you wanna send to the server but general speaking formdata is fine. Hope that helps.Luby
D
0

Set the header Content-Type to

'Content-Type': 'multipart/form-data'

Delineator answered 5/8 at 16:13 Comment(0)
T
-1

Try with solutions, It worked

In your controller update your variables as per your requirement

var a = []

$scope.b = [1,2,3,4]

for(var i =0 ; i < $scope.b.length; i++){ a.push(b[i]) }
Tibetan answered 23/11, 2015 at 12:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.