Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing PHP/JSON data in Javascript

I'm trying to communicate AJAX, JSON to PHP and then PHP returns some data and I'm trying to parse it with Javascrpt.

From the php, server I return,

    echo json_encode($data); 

    // it outputs ["123","something","and more something"]

and then in client-side,

success : function(data){

    //I want the data as following

    // data[0] = 123
    // data[1] = something
    // data[3] = and more something
}

But, it gives as;

        data[0] = [ 
        data[1] = " 
        data[2] = 1

It is reading each character but I want strings from the array, not individual characters. What is happening here? Thanks in advance, I am new to Javascript and JSON, AJAX.

like image 674
Tom Avatar asked May 31 '12 10:05

Tom


1 Answers

JSON.parse(data) should do the trick.

like image 156
Christoph Grimmer-Dietrich Avatar answered Sep 18 '22 00:09

Christoph Grimmer-Dietrich