32

I'm experimenting with sending a form to a controller. jQuery documentation says that .serializeArray() should send a json array, and .serialize() should create a query string.

However, when I try it, and inspecting with IE9 F12-mode, it looks like a query string, in both cases. Which ever call I make...

What am I missing?

Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26
kaze
  • 4,299
  • 12
  • 53
  • 74

1 Answers1

47

serializeArray creates an array (not a "json array" -- there is no such thing); you can test this yourself with console.log($("#myform").serializeArray()). On the other hand, serialize creates a query string that's meant to be part of an HTTP request. Both representations are equivalent in the sense that using appropriate code you can convert one to the other without any ambiguity.

The reason for both versions being available is that serialize is more convenient when you just want to make an HTTP request (just put the result in the query string) while serializeArray is more convenient if you want to process the results yourself.

jQuery's AJAX methods don't care if you give them one or the other because they detect the type of the parameter and convert it to a query string if it's not one already, so by the point the request is made outside observers cannot tell what was the original format of the parameters.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 3
    Ok! But - what's the point of having both serialize() and serializeArray()? – kaze May 03 '12 at 11:39
  • 12
    @kaze: That's like asking what is the point of having both sports cars and station wagons. If the only thing you 're going to use them for is driving to the mall 500m down the road there is no difference, but for most other things you need to use the right tool for the job. – Jon May 03 '12 at 11:44
  • 1
    I see now. I've tried in console.log, and I see the difference. I was puzzled that I could not see any difference in my ajax-call. – kaze May 03 '12 at 11:46
  • 6
    Why is this the accepted answer when it doesn't answer the question? The question is "what's the difference between .serialize() and .serializeArray()?" This answer is just an explanation of what serializeArray does. – Mark Simpson Nov 09 '13 at 04:02
  • 12
    @MarkSimpson: One creates an array, the other creates a (query) string -- that's clear IMO. I don't think it's fair to say "the answer doesn't address the question/is an explanation of `serializeArray`" because 1) the question text makes it clear the the OP wants to know *why both appear to do the same thing* and 2) the second paragraph explains exactly that. – Jon Nov 09 '13 at 12:26
  • @Jon "One creates an array, the other creates a (query) string -- that's clear IMO". That makes it perfectly clear, thanks :) That isn't in your answer though. – Mark Simpson Nov 09 '13 at 12:40
  • So if I'm passing images I want to use serializeArray(), not serialize()? – a coder May 17 '15 at 02:33
  • 1
    Serialize() creates a json string (plain text), SerializeArray() creates an array of objects, not a text array, you will have an array of objects each one with 'name' and 'value' keys corresponding to the form field. – lisandro Oct 19 '19 at 13:59
  • Sure there's a JSON array. #7 page 3: https://www.ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf – xr280xr Apr 30 '21 at 18:17