Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm using JQuery to make a JSON request back to the server and it seems that it's parameter serialization is hard-coded to what PHP expects instead of being generic in nature. Basically I have an object that looks like this:

{
    foo: 1,
    bar : [1, 3, 5]
}

And it serializes it to:

foo=1&bar[]=1&bar[]=3&bar[]=5

Is there anyway to make it just do?

foo=1&bar=1&bar=3&bar=5

It seems to me that jQuery shouldn't be so tied to what a handful of server side frameworks expect as a naming convention. If I wanted my param to be called bar[] I could easily name it that myself if it's what my server-side code expects.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
207 views
Welcome To Ask or Share your Answers For Others

1 Answer

I'm assuming you're using JQuery 1.4. You should take a look at this: http://benalman.com/news/2009/12/jquery-14-param-demystified/

The author discusses why they made JQuery behave this way, and makes some excellent points. For example, if you don't use the "square bracket syntax", you can't pass in arrays with only a single value.

He also offers a work-around:

$.ajaxSetup({ traditional: true });

This will tell JQuery to use the non-bracket method of serialization.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...