3

I have the following array:

var idParam = ["1","2","3"];

I want to send this data as request using jQuery.ajax, what I'm doing:

        $.ajax({
        type: "GET",
        url: "Services/GetInfo.ashx",
        data: { "id": idParam },
        contentType: "application/text",
        dataType: "json",
        success: function(result)
        {
...
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            ...
        }
});

But as result I have the following ugly string: ?id[]=1&id[]=2&id[]=4 (actually it's much uglier: id%5B%5D=1&id%5B%5D=2&id%5B%5D=4).

What to do to get the normal string like: id=1&id=2&id=4 ?? Thanks

SharpC
  • 6,974
  • 4
  • 45
  • 40
mimic
  • 4,897
  • 7
  • 54
  • 93
  • it's not ugly... it should be like that... if `id=1&id=2&id=4`, this would overwrite all id's to 4... like you are just passing id=4 – Reigel Gallarde Apr 23 '10 at 01:50
  • 1
    @Reigel - That entirely depends on how your server interprets the `GET` request. – Nick Craver Apr 23 '10 at 01:55
  • 1
    Actually, not necessarily. The relevant RFCs don't prohibit multiple keys to have the same name; it really depends on the backend processor. In this example, ASP would give you an Array -- `id=[1,2,4]`. See http://stackoverflow.com/questions/1746507/authoritative-position-of-duplicate-http-get-query-keys – josh3736 Apr 23 '10 at 02:00
  • @Nick Yeah! but it would still ends up with one value isn't it?? – Reigel Gallarde Apr 23 '10 at 02:11
  • @Reigel - The answer is *maybe*, in every major platform I know of, you're able to parse the request string manually if you want to, just really depends what the server's doing. Though, the nice easy to access classes that platform provides may not handle it (even though they *should* according to the spec). – Nick Craver Apr 23 '10 at 02:17
  • It would make more sense if it just put `id=[1,2,4]` when the parameters are passed like that. It's not like parsing JSON is hard to do in any common server-side programming language. But I guess that's what `JSON.stringify()` is for. – aroth Jan 30 '13 at 06:25

1 Answers1

7

I'm assuming this is with jQuery 1.4 - You need to use the traditional: true parameter for $.ajax()

Or you can set it globally: (from $.param() docs)

As of jQuery 1.4, the $.param() method serializes deep objects recursively to accommodate modern scripting languages and frameworks such as PHP and Ruby on Rails. You can disable this functionality globally by setting jQuery.ajaxSettings.traditional = true;.

gnarf
  • 105,192
  • 25
  • 127
  • 161
  • Unfortunatly it doesn't work anymore as of jQuery 1.8. Any suggestions? You can answer here too: http://stackoverflow.com/questions/26775682/jquery-ajaxsettings-traditional-no-loger-works – Federico Bellucci Nov 06 '14 at 09:33