2

I know this question has been asked before many times but none of them helped me. I am trying to pass an array of arrays(which may or may not have arrays in themselves) through data using $.ajax(). The response received from the server should be the same as data sent (acc to the code in php) but it sends blank object. Please help me knowing the correct syntax of passing such an array.

val_pass is an array of arrays and #display_message is paragraph to append on. Here goes my code:

 $.ajax({
        type: 'POST',
        url: 'http://someurl.com/',
        // data: JSON.stringify(val_pass),
        // data: JSON.stringify({paramName: val_pass}),
        // data: {'myArray': val_pass},
        // data: $.toJSON(val_pass),
        data: {val_pass: val_pass},
        // data: $.serialize(val_pass),
        success: function(response){
            console.log(response);
            $('#display_message').append('Data successfully passed');
        },
        error: function(xhr, status, errorThrown){
            console.log(xhr+status+errorThrown);
            alert("Sorry");
        },
    });

PS: commented lines are various ways i tried to pass an array. I am new to ajax please ignore my noobness.

Pragya Sharma
  • 94
  • 1
  • 2
  • 9
  • can you add your relevant php-code too ? – empiric Dec 09 '14 at 12:06
  • 1
    What is the problem? Use JSON.stringify: `JSON.stringify([[1,2,3],[4,5,6]])` => "[[1,2,3],[4,5,6]]". I think you need more attention to your php code, to parse a response as a array of arrays. – Alex Dec 09 '14 at 12:07
  • i tried using this by seeing similar questions and answers and it worked for this simple array var info = []; info[0] = 'hi'; info[1] = 'hello'; $.ajax({ type: "POST", data: {info:info}, url: "index.php", }); – Pragya Sharma Dec 09 '14 at 12:11
  • Use the `JQuery.parseJSON(str)` function to convert the string returned by `JSON.stringify` into an actual javascript object, then set the object as the data property in your ajax call and set dataType to json – Akshay Takkar Dec 09 '14 at 13:58
  • Eg: `$.parseJSON('{ "0" : "1,2,3", "1" : "4,5,6" }')` – Akshay Takkar Dec 09 '14 at 14:04
  • Check this http://stackoverflow.com/questions/11747373/how-to-pass-multi-dimensional-array-with-jquery-ajax-post?answertab=active#tab-top – Yatin Mistry Dec 11 '14 at 04:52

2 Answers2

0

If you are using Json then type should be json. And the function parameter for success namely json is the variable that holds the data. So data: json

 $.ajax({
 url: url_to_load,
 dataType: 'json',
 data: json,
 success: function(json){
 };
 });

A more user friendly version is as follows:

$.getJson("service.php",function(json){
//code goes here
});
Ashutosh Nigam
  • 868
  • 8
  • 27
-1

I got the solution for the problem. Firstly instead of passing arrays of arrays, I sent it as an array of objects.

Now my val_pass is array of objects.Code:

var jObject={};
jObject = JSON.stringify(val_pass);

$.ajax({
    type: 'POST';
    data: {jObject: jObject};
    url: some_url;
});

http://pavanarya.wordpress.com/2012/09/09/calling-a-webservice-using-jquery-and-passing-jsonarray-object/

referred this link for help.

Pragya Sharma
  • 94
  • 1
  • 2
  • 9