0

In my ajax code:

$.ajax({
        url: CI_ROOT + "isUserExist",
        type: "GET",
        data: {recepient: recepient},
        success: function(r) {
            console.log(r)
        }
})

Gives me an output [{"records":"1"}][{"records":"1"}] So I parsed it to json by adding dataType: "json" in my ajax code. But when I parsed it, it doesn't give me output but error on try-catch-block.

How do I get it to display as objects? In my PHP code, I'm doing it this way:

 for ($i = 0; $i < count($matches[0]); $i++) {
     echo json_encode($this->searchmodel->doesUsersExists($matches[0][$i]));
 } //gets the user id of the user from a given string.
Peter Wateber
  • 3,824
  • 4
  • 21
  • 26

2 Answers2

5

Add each entry to an array and then json encode that array, instead of json encoding each one separately. If you only have one call to json_encode, you will get valid JSON:

$result = array();
for ($i = 0; $i < count($matches[0]); $i++) {
     $result[] = $this->searchmodel->doesUsersExists($matches[0][$i]);
} //gets the user id of the user from a given string.

echo json_encode($result);
Paul
  • 139,544
  • 27
  • 275
  • 264
2

That's not valid JSON. Make an array from your exist results and encode that.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358