PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 1, 2022

[FIXED] unexpected non-whitespace character after JSON data?

 January 01, 2022     codeigniter, javascript, jquery, json, php     No comments   

Issue

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

Solution

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



Answered By - Shrikant Sharat
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing