当我使用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