Opened 9 years ago
Closed 9 years ago
#14817 closed bug (notabug)
Sending complex object via AJAX
Reported by: | LAci | Owned by: | LAci |
---|---|---|---|
Priority: | undecided | Milestone: | None |
Component: | unfiled | Version: | 1.11.0-rc1 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
If you would like to send a complex object via AJAX, the generated parameter in mimetype application/x-www-form-urlencoded is not correct. The server component (e.g Spring/Struts) is unable load load it. This is the JSON format of the object: {"prefix":[{"attr1":"MyName","attr2":"MyEmail","attr3":"MyAge"},{"attr1":"YourName","attr2":"YourEmail","attr3":"YourAge"}]}
Generated format is: prefix[0][attr1]=MyName&prefix[0][attr2]=MyEmail&prefix[0][attr3]=MyAge&prefix[1][attr1]=YourName&prefix[1][attr2]=YourEmail&prefix[1][attr3]=YourAge
The correct format would be: prefix[0].attr1=MyName&prefix[0].attr2=MyEmail&prefix[0].attr3=MyAge&prefix[1].attr1=YourName&prefix[1].attr2=YourEmail&prefix[1].attr3=YourAge
Associated array ([attr1]) is not handled but it would be good with object format (.attr1).
Correction in 1.11 version: line 9495 buildParams( prefix + "+ name +?", obj[ name ], traditional, add ); to buildParams( prefix + "." + name + "", obj[ name ], traditional, add );
Correction in 2.0 version: line 8368 buildParams( prefix + "+ name +?", obj[ name ], traditional, add ); to buildParams( prefix + "." + name + "", obj[ name ], traditional, add );
Change History (4)
comment:1 Changed 9 years ago by
comment:2 Changed 9 years ago by
Owner: | set to LAci |
---|---|
Status: | new → pending |
Can you point to a governing standard for serializing complex objects to www-form-urlencoded?
comment:3 Changed 9 years ago by
Status: | pending → new |
---|
In www-form-urlencoded both are fine. It depends on the content. (Note: object or array).
Because we do not use associative array, and the server side application (in Java) calls the setters based on the incoming names (with the values as parameters). We need "." separator here. I think it comes from one of Java/JSP standards.
I understand the problem: there is no big differences between associative arrays and objects in JavaScript. This code could not know which was the original data structure. So the implemented code is correct and this issue is not a bug! But it could be better (a feature) if jQuery developers could define somehow (with an optional parameter) the mapping format (for each level objects).
For example: buildParams(prefix, name, traditional, add, listOfItemsWhichAreAssociativeArrays)
So, please, close this issue, and create a new one as improvement with the right deseign based on this discussion.
Thanks
comment:4 Changed 9 years ago by
Resolution: | → notabug |
---|---|
Status: | new → closed |
The serialization logic built into jQuery is meant to allow simple form-like structures to be sent as if they were coming from a web page. This use case of complex nested structures isn't typical and can't occur in an HTML form.
There is already a standard way to serialize a complex JavaScript object to a string, and that is JSON. What you are requesting is that jQuery add a new non-standard way, and I can't think of a reason to do that.
http://jsfiddle.net/As6sT/