简体   繁体   中英

JSON.parse: unexpected character

i am trying to return json encoded array to a div from PHP. i receive JSON.parse: unexpected character. any help is appreciated.

Query

  <script type="text/javascript">
  $(document).ready(function(){
  var acct =$('#acct').val();

  $.getJSON("CJS/jsontest.php",{acct: acct}, function(data){
    $('#result').html(data);
  });

});
</script>

PHP

<?php
  include_once '../Functions/dbconnect.php';


$varacctname = $_REQUEST['acct'];
$varViewContacts = mysql_query("SELECT * FROM contacts WHERE c_company = '$varacctname'");

$rows = array();

  while ($row = mysql_fetch_assoc($varViewContacts)) {

    $rows[] = $row;

  }
echo json_encode($rows);

MY JSON

[{"c_ID":"21","c_FirstName":"Mike","c_LastName":"Be","c_Phone":"123456789","c_ext":"0","c_fax":"0","c_address1":"","c_address2":"","c_city":"","c_state":"","c_zip":"0","c_country":"Account","c_Email":"mike.be@test.com","c_Mobile":"123456789","c_company":"TEST"}]

Are you setting the content type anywhere in your PHP code before you send the data. I don't see it in your example.

You should have: header('Content-Type: application/json');

I have run a simple test with your code and it works fine for me. Here is the code.

var JSON = '[{"c_ID":"21","c_FirstName":"Mike","c_LastName":"Be","c_Phone":"123456789","c_ext":"0","c_fax":"0","c_address1":"","c_address2":"","c_city":"","c_state":"","c_zip":"0","c_country":"Account","c_Email":"mike.be@test.com","c_Mobile":"123456789","c_company":"TEST"}]';

var parsed = $.parseJSON(JSON);

console.log(parsed);

I am using latest version of JQuery and it interprets your JSON the right way.

So in my opnion $.getJSON() method is not able to recognize the incoming JSON. If you get your data as text and then parse it by $.parseJSON() (don't forget to include JQuery's latest version in your project), it might work for you. Kindly try it and then let me know, I will change the code accordingly.

I would first try to send dummy data such as (adjust as needed - you get the idea):

echo json_encode(array(array('name' => 'Joe'))); 

If that works then I would suspect some character encoding issue coming out of the database and start looking at utf8 converter functions (they abound online for php json encoding issues). The browser console might be sterilizing things which might explain why it works directly.

If it doesn't work then I would make a single php page with just the echo line above to ensure that nothing else was sent (and don't add a closing php tag - safer that way)

I would also investigate your php version - some have issues with blank strings from what I understand. You could upgrade or try using the alternative json_encode functions that were made for earlier php versions such as this one.

Very weird, but when recreated the PHP file the problem seems to went away.. not sure but i think it has to do something with editing with NotePad++ .. The second time i created the file with Submlime text and it was fine. so it could be encoding issue.. Thanks everyone for their input.

Probably you get invalid JSON string response. I just solve the similar issue this morning. And I desided to check(try/catch) the ajax response string(if it can be parsed) first instead of a direct calling of JSON property(it's jsonRes.result in my case). So try to put the following code into your AJAX complete callback function:

    complete: function(data) {
        var jsonRes = null;
        try {
            jsonRes = JSON.parse(data.responseText);
        }
        catch(e) {
            jAlert('Invalid JSON object);
        }
        if (jsonRes===null || jsonRes.result===undefined) {
            jAlert('Invalid Ajax response structure');
        }
    }

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