简体   繁体   中英

unexpected non-whitespace character after JSON data?

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

I found that If you are working in development mode in CI4, response will come with and extra javascript and style text, so when you try to parse to json it throw that error unexpected non-whitespace character after JSON data

You can see the extra text if you look at the response in the network tab in your browser See example

Just switching to production mode in the .env file of your project will solve it.

If somebody have a solution of how to disable this text in development mode would be great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM