當我使用json_encode($array)
我得到的數據正常,但是當我使用的是循環出現以下錯誤陣列中json_encodeAJAX失敗,而從同時獲得數據()循環在PHP
[object Object] parsererror SyntaxError: Unexpected token {
我使用Ajax來得到的functions.php
$(function() {
$('#get').click(function(){
$.ajax({
url: 'http://android.ezinfotec.com/functions.php',
type : 'GET',
data : 'method=getquestions',
dataType : 'json',
success : function(s) {
console.log(s);
},
error: function(XMLHttpRequest,textStatus,errorThrown)
{
console.log(XMLHttpRequest+' '+textStatus+' '+errorThrown);
}
});
});
});
的functions.php的JSON數據
<?php
header('Content-type: application/json');
include("connect.php");
if($_GET['method'] == 'getquestions')
{
$query = mysql_query("select * from questions");
while($fetch = mysql_fetch_array($query))
{
$output = array(
"id" => $fetch['id'],
"answers" => $fetch['answers'],
"status" => $fetch['ans_status'],
"postedon" => substr($fetch['month'],0,3).' '.$fetch['day'].' '.$fetch['year'],
"question" => $fetch['question'],
"category" => $fetch['category'],
"parent" => $fetch['parentcategory'],
"authorid" => $fetch['author'],
"authorname" => $fetch['author_name']
);
echo json_encode($output);
}
}
在上面的php代碼中,如果我刪除while循環並簡單地爲變量定義自定義值,我會在html頁面中獲得完美的數據。
注意:沒有跨域問題,因爲我有很多的功能,除了getquestions();
您可以在http://android.ezinfotec.com/functions.php?method=getquestions
'mysql_'被折舊...使用'mysqli_'代替! – NoobEditor
您可能沒有完全閱讀我的問題。檢查最後一行你可以找到json輸出。 –
爲什麼你不想一次發送所有數據? – MTahir