1

I make a $.post request to submit data and return invalid data. Here is the $.post request:

    $('#submitAll').click(function(){
        $.post("php/entries/submitAndReload.php", {array : dataObject.dataArray}, function(data){

            alert(data); // alerts: "[[“0”, “0”,””,””, “0”, “0”, “0”, “0”,”No Style”]]"
            dataObject.dataArray = data;

            $.post("php/entries/stageArea.php", {array : dataObject.dataArray}, function(data){
                $('#stageArea').html(data);
            });
        });
    });

dataObject.dataArray is a double array and alert(data) alerts what looks like the proper format for the subsequent $.post request, but the output from the 2nd $.post request looks like I pass in the following array:[[ "[" ]]. The first field gets a "[" and no other fields get data.

I'm not sure what's going on here and how to properly store the returned data into dataObject.dataArray

What's going on here?

1
  • data is a JSON string, not an array. You need to parse it. Commented Sep 22, 2015 at 20:44

2 Answers 2

2

Correct this line:

dataObject.dataArray = data;

To this:

dataObject.dataArray = JSON.parse( data );

You need to parse JSON, untill parsing it is just a string.

1

Add dataType argument to $.post.

When set as 'json' jQuery knows to parse it to object/array from json string

$.post(url, postData, function(data){
     // handling code
     alert($.type(data)); //"array"

},'json');

If you set proper content Type header at server also it helps

Reference: $.post docs

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.