Php Ajax JSON不返回数据
我试图使用jQuery/Ajax调用一个PHP文件,以JSON格式返回所有必需的数据,但是页面被屏幕底部的文本“loading”卡住了。 我读过很多其他关于这个的帖子,但是没有一篇能帮我解决这个问题Php Ajax JSON不返回数据,php,jquery,ajax,json,Php,Jquery,Ajax,Json,我试图使用jQuery/Ajax调用一个PHP文件,以JSON格式返回所有必需的数据,但是页面被屏幕底部的文本“loading”卡住了。 我读过很多其他关于这个的帖子,但是没有一篇能帮我解决这个问题 $(document).ready(function() { $.ajax({ type: 'POST', dataType: 'json', url: 'php/process.php', data: data, success: function(da
$(document).ready(function() {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'php/process.php',
data: data,
success: function(data) {
$('.data').html(data['name']);
}
});
});
只需在页面中包含PHP文件就可以毫无问题地响应JSON,它只是通过jQuery/Ajax调用它,这对我来说并不适用
<?php
$mysqli = new mysqli('localhost', 'test', 'pass@word1', 'test');
$sql = "SELECT * FROM test_json";
$result = $mysqli->query($sql);
$data = array();
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
array_push($data, $row);
}
}
echo json_encode($data);
?>
非常感谢您的帮助。谢谢。您可以尝试添加标题
<?php
header('Content-Type: application/json; charset=utf-8');
从Inspector中,我没有看到对“process.php”的请求我确实看到了一个fetchposts()
fetchposts返回3行提要和一个内容长度为5字节的零您是否查看了浏览器控制台或浏览器开发人员网络工具?在
json_encode
之后,$data
中有什么内容?哦,您是对的。结果是jQuery Mobile为一些无关的东西抛出了一个错误。现在工作!感谢您的回复。Inspect元素在控制台中显示了一个来自jQuery Mobile的错误,该错误完全无关。修好了,现在一切正常!但是,在“data:data”行上的数据仍然存在一个ReferenceError,是否不需要这一行?
echo sql_error();
print_r($data);