php json_encode() produces parse errors

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
pizzipie
New php-forum User
New php-forum User
Posts: 19
Joined: Sun Aug 04, 2019 4:10 pm

Fri Jan 03, 2020 2:49 pm

HI,

Using $Ajax to access a .php program to get data from a database. I have used basically this same code before without this problem.

Code: Select all

script type="text/javascript" >

    var frm = $('#dbForm');

    frm.submit(function (e) {

        e.preventDefault();
        
          var request=$.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),         // get-DbInfo.php
            data: frm.serialize(),
            dataType:"json"      // "json" creates parse error- if I use  "text" is ok type of json is string
         });
            
     request.done(showData );
     request.fail(showError); 
              
            function showData(data) {
            	alert("where are you people");
                console.log('Submission was successful.');
                alert(data);
            }
            
            function showError( jqXHR, textStatus ) {
                console.log('An error occurred Idiot.');
                alert(textStatus);
                //console.log(data);
            }
        
    });
</script>
The code from the server side get-DbInfo.php is:

Code: Select all

$sql=SQLite3::escapeString($sql);
$sql2=SQLite3::escapeString($sql2);

$res = $db->query($sql);
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {	
	$rows[]=$row;
}
  
//myprint($rows); //bye(103);  
    
$db->close();

} // isset $_POST

chdir($orgdir);


 echo json_encode($rows);
 //echo "'".$jsonStr."'";            // quoting json string dosen't work
Since there are 63 records in the example in the attachment I only show the first one.
Attachments
jsonStr.png
jsonStr.png (27.31 KiB) Viewed 199 times
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 927
Joined: Mon Feb 22, 2016 5:52 pm

Sat Jan 04, 2020 4:07 am

I have used basically this same code before without this problem.
What is different between the previously used code and this?
php json_encode() produces parse errors
What errors are you getting?
pizzipie
New php-forum User
New php-forum User
Posts: 19
Joined: Sun Aug 04, 2019 4:10 pm

Sat Jan 04, 2020 8:17 am

parseerror.png
parseerror.png (40.04 KiB) Viewed 183 times
This is the error I get.

If I use "text" for the 'dataType' in the $Ajax call the response is ok. I guess the parserror is for a mis-formed json. I checked with gettype(json_encode($rows) and the type is 'string'. I was under the assumption that the return would be a json array.
User avatar
hyper
php-forum GURU
php-forum GURU
Posts: 927
Joined: Mon Feb 22, 2016 5:52 pm

Sat Jan 04, 2020 8:49 am

What is the difference in the code you used before?
pizzipie
New php-forum User
New php-forum User
Posts: 19
Joined: Sun Aug 04, 2019 4:10 pm

Sat Jan 04, 2020 9:03 am

There is basically no difference. What is returned is this:
[{"Id":1,"Date":"04\/03\/2019","Location":"1540 Woodlawn","Vendor":"Lowes","Description":"Kitchen Sink And Faucet","Category":"appliances","PaidBy":"Lowes","AcctNo":"9800","Amount":438.5},{"Id":2,"Date":"04\/21\/2019","Location":"1540 Woodlawn","Vendor":"Lowes","Description":"Refrigerator And BR Light","Category":"appliances","PaidBy":"Lowes","AcctNo":"9800","Amount":1567.55},{"Id":3,"Date":"05\/03\/2019","Location":"1540 Woodlawn","Vendor":"Lowes","Description":"Washer Dryer","Category":"appliances","PaidBy":"Lowes","AcctNo":"9800","Amount":1102.1}]
I don't know a lot about json but this looks like a valid json string to me. Would the back slashes (\/)in the dates cause a problem? I'm going to take them out and see if that makes a defference.

Well I took the back slashes out and it made no differemce. Here is what I'm send back to the calling $Ajax.
[{"Id":1,"Date":"04/03/2019","Location":"1540 Woodlawn","Vendor":"Lowes","Description":"Kitchen Sink And Faucet","Category":"appliances","PaidBy":"Lowes","AcctNo":"9800","Amount":438.5},{"Id":2,"Date":"04/21/2019","Location":"1540 Woodlawn","Vendor":"Lowes","Description":"Refrigerator And BR Light","Category":"appliances","PaidBy":"Lowes","AcctNo":"9800","Amount":1567.55},{"Id":3,"Date":"05/03/2019","Location":"1540 Woodlawn","Vendor":"Lowes","Description":"Washer Dryer","Category":"appliances","PaidBy":"Lowes","AcctNo":"9800","Amount":1102.1}]
pizzipie
New php-forum User
New php-forum User
Posts: 19
Joined: Sun Aug 04, 2019 4:10 pm

Sat Jan 04, 2020 10:17 am

WELL ......... MY STUPID MISTAKE !!!!

I got to really studying what was returned and I discovered that the debugging strings that i had echoed to the screen were actually being passed on along with the json_encode string. That is the data passed back to the $Ajax call actually was 'garbage . json_encode string'.

Sorry to waste your time with this.

R
Post Reply