0

I am running a database query in php controller and returning the result to an Ajax function. Here's the php code.

public function trackUnreadMsgs(){
        $data['userData']  = $this->session->userdata('userData');
        $user_id           = $data['userData'][0]['id'];
        $QueryResult = $this->data->myquery("SELECT * FROM inbox WHERE `to`=" . $user_id . " AND status='unread'");
        $QueryResult = json_encode($QueryResult);
        print_r($QueryResult);
}

Here's my function in ajax:

var colourNewMessage = urlToPass + '/home/trackUnreadMsgs';
$.ajax({
    url: colourNewMessage,
    context: document.body,
    success: function(result){
             console.log(result);
    }
});

My Question is that how can I convert the returned json object to javascript array.

Danish Sofwan
  • 333
  • 3
  • 9

1 Answers1

1

If you have a well formed JSON object say json, then all you need to do to convert it to JavaScript object is to use

var jsobj = JSON.parse(json)
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400