.type('form') doesn't prepare Javascript arrays correctly
See original GitHub issuePlease let me know if I am doing something wrong, but if I say something like:
var data = {
interests: [300, 400]
}
request('/foo')
.type('form')
.send(data)
.end()
The form data is sent as
interests: 300, 400
when it should be
interests[]: 300
interests[]: 400
I am able to get it to work if I do:
request('/foo')
.type('form')
.send($.param(data))
.end()
but the reason I’m using superagent is so this will also work on Node and I’d rather not require in Node jQuery just for this.
With that said, is there a setting I’m missing that is not preparing the params correctly?
Issue Analytics
- State:
- Created 10 years ago
- Comments:10 (3 by maintainers)
Top Results From Across the Web
type('form') doesn't prepare Javascript arrays correctly #250
Please let me know if I am doing something wrong, but if I say something like: var data = { interests: [300, 400]...
Read more >JS not pushing to array - javascript - Stack Overflow
Upon further inspection: the prepare() function is looking for an element with id="seatsArray", which doesn't exist.
Read more >Discover JavaScript Arrays & How to Use Them - HubSpot Blog
Discover JavaScript arrays, how they work and what that means for your software ... What is the correct way to write a JavaScript...
Read more >JavaScript Arrays - tips, tricks and examples - CodinGame
Arrays are just regular objects In Javascript, there are only 6 data types defined – the primitives (boolean, number, string, null, undefined) and...
Read more >Check if the value exists in Array in Javascript - Javatpoint
In a programming language like Javascript, to check if the value exists in an array, there are certain methods. To be precise, there...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This is intentional and IMHO correct.
The brackets
[]
are only an informal convention used by some frameworks/servers, but not all environments use it. There are web servers that expect repeated keys without brackets:If superagent always added
[]
to arrays, then it would not be able to create a URL like the one above.The current implementation supports both cases. If you’d like an array with
[]
in the name, then put[]
in the name explicitly:Please note that jQuery.param() also has special treatment for object data.