开发者

unexpected non-whitespace character after JSON data?

开发者 https://www.devze.com 2023-04-09 05:58 出处:网络
I want in output this PHP code echo name, star_type, service by jquery.each(), but i have error. how is fix it?

I want in output this PHP code echo name, star_type, service by jquery.each(), but i have error. how is fix it?

error:

An error has occured:

[object Object]

parsererror

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data

I have this PHP code:

//$hotel_id = $this->input->post('hotel_id');
$开发者_StackOverflow社区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:

[{
    "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:

$.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);
    }
});


You are not echoing valid json. Try this:

$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:

$.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 :)


Your php output is not valid json, you missed a comma before "address".

You can check your json with this url: http://json.parser.online.fr/


Your JSOn is completely invalid. You shouldn't echo json-ecnoded array inside the loop, but outside it:

$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);
0

精彩评论

暂无评论...
验证码 换一张
取 消