<anonymous>上的JQuery parseerror

时间:2016-12-16 07:12:56

标签: php jquery json ajax

从php对象获取json。单独测试它,PHP返回正确形成的json,并没有任何迹象。我在网上搜索了建议,并查看了文档,但没有找到解决方案。

谢谢!

在控制台中,出现以下错误:

parsererror SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at jQuery.parseJSON (jquery-1.11.2.js:8515)
    at ajaxConvert (jquery-1.11.2.js:8841)
    at done (jquery-1.11.2.js:9259)
    at XMLHttpRequest.callback (jquery-1.11.2.js:9713)

代码段:

<script>
    $(document).ready(function() {
        $('#submit').click(function(){
            alert('Loading data...');
            $.ajax({
                 url: "jsonData.php",
                 type: "GET",
                 data: {},
                 dataType: "json",
                 success: function(data) {
                      alert('Presenting data...');
                      $("#result").html(data);
                  },
                 error: function(jqXHR, textStatus, errorThrown) {
                  console.log(textStatus,errorThrown);
                 }
         });
     });
});
</script>

结果是一个简单的div

   <div id="result" class="well"></div>

JSON:

{"data":[{"key":"1002","sc":"3214","date":"15-Dec-2016 10:15 AM"},{"key":"1003","sc":3215"","date":"15-Dec-2016 10:17 AM"}]}

2 个答案:

答案 0 :(得分:1)

您的JSON无效。

{"key":"1003","sc":3215"","date":"15-Dec-2016 10:17 AM"}

"sc":3215""应为"sc":"3215"

答案 1 :(得分:1)

也许您的Json响应(无效)

{
    "data": [{
        "key": "1002",
        "sc": "3214",
        "date": "15-Dec-2016 10:15 AM"
    }, {
        "key": "1003",
        "sc": 3215 "",
        "date": "15-Dec-2016 10:17 AM"
    }]
}
  

&#34; sc&#34;:3215&#34;&#34;,不正确

应该是这样的

{
    "data": [{
        "key": "1002",
        "sc": "3214",
        "date": "15-Dec-2016 10:15 AM"
    }, {
        "key": "1003",
        "sc": "3215",
        "date": "15-Dec-2016 10:17 AM"
    }]
}
  

&#34; sc&#34;:&#34; 3215&#34;,应该是这样的