New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to append an object? #92
Comments
Hey, The thing is you can send only strings over the wire, so complex data types, like object should be encoded somehow and since there are many way to encode objects, FormData leaves it to developer to choose proper encoding mechanism that supported on the other end. For example it could be a Something like:
Let me know if you have more questions. |
Hello, I've already tried JSON.stringify but unfortunately, the field was arriving as a string instead of an object. I don't want to add into my production code logic to parse some of the fields from string to json just because of tests:( |
You'd need to handle decoding one way or another and because implementations differ from server to server, you need to find combination that would work for your setup. One of the ways is to stay away from nested structure, something like:
or
or PHP-style (not sure if Hapi would support it out of the box):
Another solution will be to encode all your data as JSON and send it to the server with proper headers, |
Thanks php style helped for hapi, I thought that I' ve already tried this, but obviously I had some bug there before :) |
How about if It wouldn't be worse than the current behavior of |
it maybe common case for some apps, but some other apps work with url-encoded data, and even with url-encoding there are multiply ways, like standard HTTP way and PHP array-friendly way. And if all you need is Throwing errors in general meant for developer errors, to notify developer of the unacceptable behavior. And for developers it's much easier to find out about the problem this way, then somewhere deeper in stack because some library took freedom to assume things for the developer, especially for dynamic languages like JS, where you can accidentally pass object instead of a string. |
Excellent points, I agree. |
Is there any npm module for transforming an object to PHP style array? recursively |
i can not append list of objects to my form data i got empty list on the action
|
I use Swagger2.0, mongoDB as database and I am trying to go with the stringify() during sending and decode in the Flask server side. |
I was trying to post a complex data structure without pickling it into json to set up a simple data flow from React to Rails, and this thread was the most helpful piece of information. Just in case someone is looking for an example: data to be posted:
implementation with
|
can I append the Object as it is, without Stringify it? |
@abned08 no, you may stringify it or use @alexsmartens's solution. FormData.append() only accepts |
Thanks a lot to @alexsmartens for example. I was 3, 4 days without can send objects in FormData post and his example really help me to success. I believe that @alexindigo is elegant solution but I not sure how implements in my case. ( Use enctype: 'multipart/form-data' and contentType: false). greetings :) |
Following @alexsmartens example here's a handy js function to append an object to
And here's is how you would call it:
It works great calling ajax from js to PHP. |
In case this is useful for somebody else, in the case you are using |
Hello need to append object field something like:
but it will throw an error like this:
The text was updated successfully, but these errors were encountered: