【发布时间】:2021-10-25 01:28:23
【问题描述】:
所以基本上我遇到了 ajax 调用没有检索 JSON 数据的标准问题。那将是它是一个数组,而您没有使用索引。但我 AM 在前端使用索引。我为此看到的所有问题都没有奏效,(其中大多数是“使用索引”)并且我查看了文档。我试过直接瞄准数据库,但我目前正在使用一个 php 文件。我已经尝试过使用 $.getJSON 的标准方式(它什么都不返回),所以我尝试将它定义为一个变量并返回 [object Object] 或未定义(我的代码中仍然有两者)。现在,对于一些代码 数据库.json
[{"Usrname":"Admin","Comment":"Comments Are Working!"},{"Usrname":"billy","Comment":"y u no work"},{"Usrname":"jrnvreifnwrg","Comment":"vfrjinoiewg"},{"Usrname":"bill","Comment":"testing 123 testing"},{"Usrname":"James","Comment":"Now all that has to be done it to show the text in a chat box"},{"Usrname":"test","Comment":"hi there pls work"},{"Usrname":"Benjamin","Comment":"Hello"},{"Usrname":"Benjamin","Comment":"Hello"},{"Usrname":"Bill","Comment":"Hello"}]
- 我试过用引号和不用引号,似乎没有任何改变
get.php
<?php
header('Content-Type: application/json');
$data = file_get_contents('database.json');
echo $data;
?>
contact.html (只是一个 sn-p,评论 div 在那里并且正在运行)
<script>
$(document).ready(function () {
response = $.getJSON('https://ambitioussociableprotools.babyboy666.repl.co/get.php')
document.getElementById("comments").innerText += response
document.getElementById("comments").innerText += response[0]
});
</script>
<script>
$.getJSON("https://ambitioussociableprotools.babyboy666.repl.co/get.php", function(data){
document.getElementById("comments").innerText += data[0].Usrname
});
</script>
<script>
data = $.get("https://ambitioussociableprotools.babyboy666.repl.co/get.php")
document.getElementById("comments").innerText += data
document.getElementById("comments").innerText += data[0].Usrname
var obj = JSON.parse(data)
document.getElementById("comments").innerText += obj
document.getElementById("comments").innerText += obj[0]
document.getElementById("comments").innerText += obj[0].Usrname
</script>
【问题讨论】: