Php JSON数据后出现意外的非空白字符?
我想通过Php JSON数据后出现意外的非空白字符?,php,javascript,jquery,codeigniter,json,Php,Javascript,Jquery,Codeigniter,Json,我想通过jquery.each()输出这个PHP代码echoname,star\u type,service,但我有错误。如何修复它 错误: 出现错误:[对象对象]解析器错误 SyntaxError:JSON.parse:后面出现意外的非空白字符 JSON数据 我有以下PHP代码: //$hotel_id = $this->input->post('hotel_id'); $hotel_id = array('1','2','3'); //print_r($hotel_id); fo
jquery.each()
输出这个PHP代码echoname
,star\u type
,service
,但我有错误。如何修复它
错误:
出现错误:[对象对象]
解析器错误
SyntaxError:JSON.parse:后面出现意外的非空白字符 JSON数据
我有以下PHP代码:
//$hotel_id = $this->input->post('hotel_id');
$hotel_id = array('1','2','3');
//print_r($hotel_id);
foreach ($hotel_id as $val) {
$query_r = $this->db->query("SELECT * FROM hotel_submits WHERE id LIKE '$val' ORDER BY id desc");
$data = array();
foreach ($query_r->result() as $row) {
$data_s = json_decode($row->service, true);
$data_rp = json_decode($row->address, true);
$data[] = array(
'name' => $row->name,
'star_type' => $row->star . '-' . $row->type,
'site' => $row->site,
'service' => $data_s,
'address' => $row->address
);
}
echo json_encode($data);
}
[{
"name": "how",
"star_type": "5-hotel",
"site": "www.sasaas.assa",
"service": ["shalo", "jikh", "gjhd", "saed", "saff", "fcds"]"address": "chara bia paeen"
}][{
"name": "hello",
"star_type": "4-motel",
"site": "www.sasasa.asas",
"service": ["koko", "sili", "solo", "lilo"]"address": "haminja kilo nab"
}][{
"name": "hi",
"star_type": "3-apparteman",
"site": "www.saassaas.aas",
"service": ["tv", "wan", "hamam", "kolas"],
"address": "ok"
}]
这是PHP代码上方的输出:
//$hotel_id = $this->input->post('hotel_id');
$hotel_id = array('1','2','3');
//print_r($hotel_id);
foreach ($hotel_id as $val) {
$query_r = $this->db->query("SELECT * FROM hotel_submits WHERE id LIKE '$val' ORDER BY id desc");
$data = array();
foreach ($query_r->result() as $row) {
$data_s = json_decode($row->service, true);
$data_rp = json_decode($row->address, true);
$data[] = array(
'name' => $row->name,
'star_type' => $row->star . '-' . $row->type,
'site' => $row->site,
'service' => $data_s,
'address' => $row->address
);
}
echo json_encode($data);
}
[{
"name": "how",
"star_type": "5-hotel",
"site": "www.sasaas.assa",
"service": ["shalo", "jikh", "gjhd", "saed", "saff", "fcds"]"address": "chara bia paeen"
}][{
"name": "hello",
"star_type": "4-motel",
"site": "www.sasasa.asas",
"service": ["koko", "sili", "solo", "lilo"]"address": "haminja kilo nab"
}][{
"name": "hi",
"star_type": "3-apparteman",
"site": "www.saassaas.aas",
"service": ["tv", "wan", "hamam", "kolas"],
"address": "ok"
}]
这是我的js代码,它会出错:
$.ajax({
type: "POST",
dataType: "json",
url: 'get_residence',
data: dataString_h,
cache: false,
success: function (respond) {
//alert(respond);
$.each(respond[0].name, function (index, value) {
alert(value);
});
},
"error": function (x, y, z) {
alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
}
});
您没有回显有效的json。试试这个:
$hotel_data = array();
foreach(...) {
// .. do stuff
$hotel_data[] = $data; // add $data to the end of the $hotel_data array
}
echo json_encode(array('data' => $hotel_data));
这将把所有$data
数组包装成一个数组,并将其放入对象的数据属性中。您可以在js端访问此数据,如下所示:
$.each(response.data, function(i, obj) {
alert(obj.name);
});
注意:我不确定我上面写的php语法,我写php已经有一段时间了:)您没有响应有效的json。试试这个:
$hotel_data = array();
foreach(...) {
// .. do stuff
$hotel_data[] = $data; // add $data to the end of the $hotel_data array
}
echo json_encode(array('data' => $hotel_data));
这将把所有$data
数组包装成一个数组,并将其放入对象的数据属性中。您可以在js端访问此数据,如下所示:
$.each(response.data, function(i, obj) {
alert(obj.name);
});
注意:我不确定我上面写的php语法,我写php已经有一段时间了:)您的php输出不是有效的json,您在
“address”
之前漏掉了一个逗号
您可以使用以下url检查您的json:您的php输出不是有效的json,您在
“地址”
之前遗漏了一个逗号
您可以使用以下url检查json:您的json完全无效。您不应在循环内回显json ECNODE数组,而应在循环外回显:
$all_data = array();
foreach ($hotel_id as $val) {
//..what you have there now, but instead if echo json_encode($data); you do
$all_data[] = $data;
}
//and finally
echo json_encode('data'=>$all_data);
您的JSOn完全无效。您不应在循环内回显json ECNODE数组,而应在循环外回显:
$all_data = array();
foreach ($hotel_id as $val) {
//..what you have there now, but instead if echo json_encode($data); you do
$all_data[] = $data;
}
//and finally
echo json_encode('data'=>$all_data);