2016-12-16 57 views
0

從php對象獲取json。單獨進行測試,PHP返回正確形成的json,並且沒有指示。我在網上搜索了一些建議,並查閱了文檔,但沒有找到解決方案。對<anonymous>的JQuery parseerror

謝謝!

在控制檯,收到以下錯誤:

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"}]} 
+0

** data:{} **?如果您沒有要發佈的數據比爲什麼使用數據屬性? –

+0

從我嘗試的其他選項中補餘。刪除或添加對錯誤沒有影響 –

+0

你有沒有得到相同的json響應?從PHP方面? –

回答

1

您的JSON無效。

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

"sc":3215""應該"sc":"3215"

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" 
    }] 
} 

"sc": 3215 "", not proper

它應該是這樣的

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

"sc": "3215", is should like this