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.

6
  • can you add your relevant php-code too ? Commented Dec 9, 2014 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. Commented Dec 9, 2014 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", }); Commented Dec 9, 2014 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 Commented Dec 9, 2014 at 13:58
  • Eg: $.parseJSON('{ "0" : "1,2,3", "1" : "4,5,6" }') Commented Dec 9, 2014 at 14:04

2 Answers 2

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

a more user friendly? You gotta be kidding
-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.

1 Comment

And how did you handle this in PHP in the receiving page?

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.