我的 php 脚本中有一个 json_encode,如果在 while 循环中调用 json_encode,它似乎会使 Jquery 方面失败。
当我在浏览器窗口而不是页面中查看结果时,我需要结果,我可以看到它确实有效。
例如:
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['title'];
$content = $row['content'];
$data = array("id" => $id,
"title" => $title,
"success" => true
);
echo json_encode($data); // Inside the While Loop
}
浏览器中的输出是:
{"id":"15","title":"Advanced Booking Examples","success":true}
{"id":"14","title":"Advanced Booking Fields","success":true}
{"id":"11","title":"Add New Driver","success":true}
{"id":"10","title":"Registering a Driver \/ Add New Vehicle","success":true}
{"id":"8","title":"Dispatch Controls","success":true}
{"id":"7","title":"Troubleshooting Driver Device Checklist","success":true}
{"id":"6","title":"Requirements Checklist","success":true}
在 jQuery 响应中,这实际上是空白的,但是如果我将其留在echo json_encode($data);
while 循环之外,则 jQuery 会获取响应,但是只有 1 条记录 - 是循环中的最后一条记录。
我需要 jQuery 从循环中获取所有结果。
这是JS
$.ajax({ // Send it off for prcessing
type: "POST",
dataType: 'json',
url: "includes/auto_suggest.php",
data: {
q: inputString
},
success: function (result) {
if (result.success) {
var id = result.id;
var title = result.title;
$('#auto_id').append("<input type='text' class='hideid' id='hideid_" + id + "' value='" + id + "'/>");
$('#auto_title').prepend("<p>" + title + "</p>");
}
}
});
有任何想法吗?
提前致谢