javascript - JSON 输出未显示在控制台日志中
问题描述
我正在尝试为我的博客的非活动文章创建一个通知按钮,我不希望管理员重新加载他/她的页面以查看提交的新非活动文章,所以我想使用 Ajax 执行此操作,但是,我很新到阿贾克斯。我已经从数据库中获取数据,并以名为 file.php 的文件名存储在 JSON 中,这是我的代码:
require $_SERVER['DOCUMENT_ROOT'].'/config/init.php';
require CLASS_PATH.'article.php';
$article = new Article();
header('Content-Type: application/json; charset=utf-8');
$list = $article->getInactiveArticle();
echo json_encode($list);
我为 Ajax 编写了以下代码行:
<script>
$.ajax({
type: "POST",
url: 'file.php',
dataType: 'json',
success: function(response)
{
if (response != 0 ) {
if (typeof(response) != "object") {
response = $.parseJSON(response);
console.log(response);
}
}
}
});
</script>
尽管 JSON 中有数据,但我在控制台中没有得到任何东西。应该做什么?
解决方案
尽管 JSON 中有数据,但我在控制台中没有得到任何东西。
where$.ajax()
type
设置为"json"
第一个参数 atcallback
是一个 JavaScript 普通对象,而不是JSON
字符串。
应该做什么?
删除if
语句并使用console.log(response)
. JSON.parse()
没有必要。
$.ajax({
type: "POST",
url: 'file.php',
dataType: 'json',
success: function(response) {
console.log(response);
}
})
推荐阅读
- node.js - 遇到端口问题并将反应/节点项目部署到数字海洋
- save - PhpStorm SFTP 不会写入远程主机 (AWS EC2)
- android - glide 和 recyclerview 的 CPU 使用率高
- amazon-web-services - aws comprehend 可以用于将文档拆分为句子吗?
- jquery - Transformicons - 打开/关闭覆盖内容
- python - 使用python串口与Roboclaw电机控制器通信
- xslt-2.0 - 在 xslt 的 xml 字段中搜索字符串
- python - Python 在开头创建 None 的实例
- java - Eclipse PDE 缺少的功能
- php - SQLSTATE [HY000]:一般错误:1364 字段 'uID' 没有默认值