user568021
user568021

Reputation: 1476

Jquery loses brackets on data after ajax request

I'm having a data text file like this:

[[1412525998000,"91.83"],[1412525998000,"91.83"],[1412525997000,"90.14"]...ETC

When I get this data trough ajax request something weird happens. The 'data' variable becomes this:

1412525998000,91.831412525998000,91.831412525997000,90.14...ETC

For some reason it just dumps all square-brackets.

This is my ajax call:

$.ajax({
    url: "mydata.php",
    data: {},
    success: function(data) { 
        $('#debug_div').html(data);
    },
    dataType: "json"
});

I've never seen anything similar so I wonder what could be causing this?

Upvotes: 1

Views: 1193

Answers (1)

MrCode
MrCode

Reputation: 64526

You're specifying type json so jQuery is automatically parsing the response JSON and giving you the resulting array as data. You then pass it to .html() which is casting the array as a string, causing the output you see.

To demonstrate:

document.write( [1, 2, 3] );

Outputs

1, 2, 3

Your array is perfectly intact, it's only when you cast it as a string that you see the result you have. For example:

console.log(data[0][0]); // 1412525998000
console.log(data[0][1]); // 91.83

Refer to Array.prototype.toString().

Upvotes: 1

Related Questions