[英]Ajax JSON Not Returning Data
I am attempting to use jQuery/Ajax to call a PHP file returning all the required data in JSON format, however the page just gets stuck with the text "loading" at the bottom of the screen. 我试图使用jQuery / Ajax调用一个PHP文件,以JSON格式返回所有必需的数据,但是该页面仅停留在屏幕底部的“正在加载”文本中。 I have read various other posts on this but none have helped solve this issue for me.
我已经阅读了其他有关此问题的文章,但没有一篇对我有帮助。
$(document).ready(function() {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'php/process.php',
data: data,
success: function(data) {
$('.data').html(data['name']);
}
});
});
Simply including the PHP file in the page echoes the JSON with no problem, it's just calling it through jQuery/Ajax that isn't working for me. 只需在页面中包含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);
?>
Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks.
谢谢。
You might try adding a header 您可以尝试添加标题
<?php
header('Content-Type: application/json; charset=utf-8');
right click and Inspect element. 右键单击并检查元素。
Select the Network Tab 选择网络选项卡
Select XHR at the bottom of the window 在窗口底部选择XHR
check the Response Header and the Response body. 检查响应标题和响应正文。
Add 加
echo sql_error();
print_r($data);
From the Inspector I do not see a Request to "process.php" 从检查器中,我看不到对“ process.php”的请求
I do see one to fetchposts() 我确实看到了一个fetchposts()
fetchposts returns 3 line feeds and and a zero with a content length of 5 bytes fetchposts返回3个换行符和一个0,内容长度为5个字节
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.