Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 关于json编码和ajax数据类型;json";_Php_Javascript_Json - Fatal编程技术网

Php 关于json编码和ajax数据类型;json";

Php 关于json编码和ajax数据类型;json";,php,javascript,json,Php,Javascript,Json,在我的ajax代码中: $.ajax({ url: CI_ROOT + "isUserExist", type: "GET", data: {recepient: recepient}, success: function(r) { console.log(r) } }) 给我一个输出[{“记录”:“1”}][{“记录”:“1”}],所以我通过在ajax代码中添加数据类型:“json”将其解

在我的ajax代码中:

$.ajax({
        url: CI_ROOT + "isUserExist",
        type: "GET",
        data: {recepient: recepient},
        success: function(r) {
            console.log(r)
        }
})
给我一个输出[{“记录”:“1”}][{“记录”:“1”}],所以我通过在ajax代码中添加数据类型:“json”将其解析为json。但当我解析它时,它并没有给出输出,而是在try-catch块上给出了错误

如何将其显示为对象? 在我的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.
for($i=0;$isearchmodel->doesUserExists($matches[0][$i]);
}//从给定字符串获取用户的用户id。

这不是有效的JSON。从exist结果生成一个数组并对其进行编码。

将每个条目添加到一个数组中,然后对该数组进行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);
$result=array();
对于($i=0;$isearchmodel->doesUserExists($matches[0][$i]);
}//从给定字符串获取用户的用户id。
echo json_编码($result);