Pet*_*ber 0 javascript php json
在我的ajax代码中:
$.ajax({
url: CI_ROOT + "isUserExist",
type: "GET",
data: {recepient: recepient},
success: function(r) {
console.log(r)
}
})
Run Code Online (Sandbox Code Playgroud)
给我一个输出[{"records":"1"}] [{"records":"1"}]所以我通过在我的ajax代码中添加dataType:"json"将其解析为json .但是当我解析它时,它不会给我输出但是在try-catch-block上有错误.
如何将其显示为对象?在我的PHP代码中,我这样做:
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.
Run Code Online (Sandbox Code Playgroud)
将每个条目添加到数组,然后json编码该数组,而不是分别对每个数组进行json编码.如果您只有一次调用json_encode,您将获得有效的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);
Run Code Online (Sandbox Code Playgroud)