0

i'm having trouble with jQuery.param() adding an empty array key when serializing an object

payload: {
    "page":0,
    "size":30,
    "query":"",
    "facets": {
        "category": [
            "tagName1",
            "tagName2"
        ]
    }
}

What i get when i serialize payload is:

page=0&size=30&query=&facets%5Bcategory%5D%5B%5D=tagName1&facets%5Bcategory%5D%5B%5D=tagName2

url decoded:

page=0&size=30&query=&facets[category][]=tagName1&facets[category][]=tagName2

what i'm expecting is (url decoded):

page=0&size=30&query=&facets[category]=tagName1&facets[category]=tagName2 

Are my expectations wrong, is this a bug in jquery serialization or is it intended behaviour?

Thanks in advance

5
  • serialise how? Show code - for example in a snippet. serialize is for forms Commented Aug 16, 2016 at 14:59
  • The brackets [] after arrays are added to make languages like PHP happy IIRC. Passing true in the second argument to $.param() should fix that problem. Commented Aug 16, 2016 at 15:01
  • @FrédéricHamidi unfortunately if i pass true in the second argument it won't enter the object at all and just print ...&facets=[object object]... Commented Aug 16, 2016 at 15:05
  • 1
    This is actually the result specified in api.jquery.com/jquery.param - as @FrédéricHamidi wrote, you can pass true as the second argument to the function for a "shallow" serialization - but then it will not work correctly with nested objects (also described on the API page). Commented Aug 16, 2016 at 15:05
  • @Karim, well, the documentation does say Because some frameworks have limited ability to parse serialized arrays, developers should exercise caution when passing an obj argument that contains objects or arrays nested within another array. It appears you have hit that case. You will probably have better luck by using plain JSON instead of $.param(). Commented Aug 16, 2016 at 15:06

1 Answer 1

1

It appears that this is intended behaviour indeed.

I ended up doing a string replace inside the beforeSend hook

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.