javascript JSON 数据后出现意外的非空白字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7569192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
unexpected non-whitespace character after JSON data?
提问by Jennifer Anthony
I want in output this PHP code echo name
, star_type
, service
by jquery.each()
, but i have error. how is fix it?
我想在输出这个 PHP 代码 echo name
, star_type
, service
by jquery.each()
,但我有错误。怎么解决?
error:
错误:
An error has occured:
[object Object]
parsererror
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
发生错误:
[object Object]
parsererror
SyntaxError:JSON.parse:JSON 数据后出现意外的非空白字符
I have this PHP code:
我有这个 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);
}
This is output above PHP code:
这是 PHP 代码上方的输出:
[{
"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"
}]
And this my js code that get error:
这是我得到错误的 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);
}
});
回答by Shrikant Sharat
You are not echoing valid json. Try this:
您没有回显有效的 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));
This will wrap all the $data
arrays into an array and put it into an object's data attribute. You can access this data on the js side as follows:
这会将所有$data
数组包装成一个数组并将其放入对象的数据属性中。您可以在 js 端访问这些数据,如下所示:
$.each(response.data, function(i, obj) {
alert(obj.name);
});
Note:I am not sure about the php syntax I wrote above, its been a while since I wrote php :)
注意:我不确定我上面写的 php 语法,自从我写 php 以来已经有一段时间了 :)
回答by DavidEG
Your php output is not valid json, you missed a comma before "address"
.
您的 php 输出不是有效的 json,您在"address"
.
You can check your json with this url: http://json.parser.online.fr/
您可以使用以下网址检查您的 json:http: //json.parser.online.fr/
回答by J0HN
Your JSOn is completely invalid. You shouldn't echo json-ecnoded array inside the loop, but outside it:
您的 JSOn 完全无效。您不应该在循环内部回显 json-ecnoded 数组,而应该在循环外部回显:
$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);