tags:

views:

27

answers:

2

how do I pass multiple level array using jQuery ajax function?

example of data to be passed:

{"data": { "username" : "name1" , "password" : "password1" } }

in

bodyContent = $.ajax({
      url: "somepage",
      global: false,
      type: "POST",
      **data:** ,
      ...
+1  A: 

Encode it as a JSON string.

Amarghosh
isn't it already in JSON ?{"data": { "username" : "name1" , "password" : "password1" } }
Brandon
@Brandon: No, if you pass in an object { "a" : "b" }, by default it gets form-encoded if the request is POST, and url-encoded if the request is a GET
flitzwald
@Brandon - currently it's just a JavaScript object; you have to encode it to JavaScript Object Notation.
Amarghosh
thanks for explaining! you are right... Reigel gave a direct answer for your explanation :)
Brandon
$.param() does not encode in JSON format
sje397
+3  A: 

use $.param()

var params = {
    "data": {
        "username" : "name1" ,
        "password" : "password1" 
    } 
};
    var str = jQuery.param(params);
    // decodeURIComponent(str) == data[username]=name1&data[password]=password1​

here's a demo

Reigel
nice demo. now I actually know what the back-end is expecting from your commented bit.
Brandon