简体   繁体   English

JSON.parse:意外字符

[英]JSON.parse: unexpected character

i am trying to return json encoded array to a div from PHP. 我正在尝试将JSON编码数组从PHP返回到div。 i receive JSON.parse: unexpected character. 我收到JSON.parse:意外字符。 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

<?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 我的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. 发送数据之前,您是否在PHP代码中的任何位置设置了内容类型。 I don't see it in your example. 我在您的示例中看不到它。

You should have: header('Content-Type: application/json'); 您应该具有: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. 我正在使用最新版本的JQuery,它以正确的方式解释了JSON。

So in my opnion $.getJSON() method is not able to recognize the incoming JSON. 因此,在我的opnion中,$。getJSON()方法无法识别传入的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. 如果您以文本形式获取数据,然后通过$ .parseJSON()对其进行解析(请不要忘记在项目中包含JQuery的最新版本),那么它可能对您有用 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). 如果这行得通,那么我会怀疑数据库中会出现一些字符编码问题,并开始查看utf8转换器函数(它们在线上大量出现php json编码问题)。 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) 如果它不起作用,那么我将使用上面的回显行制作一个php页面,以确保没有发送其他任何内容(并且不要添加闭合的php标签-这样更安全)

I would also investigate your php version - some have issues with blank strings from what I understand. 我还将调查您的php版本-从我的理解中,有些字符串有问题。 You could upgrade or try using the alternative json_encode functions that were made for earlier php versions such as this one. 您可以升级或尝试使用为早期php版本(例如版本)制作的替代json_encode函数。

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. 很奇怪,但是当重新创建PHP文件时,问题似乎消失了。 so it could be encoding issue.. Thanks everyone for their input. 因此可能是编码问题。谢谢大家的投入。

Probably you get invalid JSON string response. 可能您收到无效的JSON字符串响应。 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). 我愿意先检查(尝试/捕获)ajax响应字符串(如果可以解析),而不是直接调用JSON属性(在我的情况下为jsonRes.result)。 So try to put the following code into your AJAX complete callback function: 因此,请尝试将以下代码放入AJAX完整的回调函数中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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