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.

8
  • This must be a duplicate. I saw this not the other day... Commented May 1, 2016 at 9:43
  • @evolutionxbox Can you please flag it then? Commented May 1, 2016 at 9:44
  • @RajaprabhuAravindasamy doing so now... I'm looking for it Commented May 1, 2016 at 9:45
  • @evolutionbox Please help Commented May 1, 2016 at 9:46
  • 1
    If you're expecting a json object as response from server then add dataType: "json" setting to your AJAX request. Commented May 1, 2016 at 9:50

1 Answer 1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.