【问题标题】:Getting "undefined" value Ajax json while i try to get values from database在我尝试从数据库中获取值时获取“未定义”值 Ajax json
【发布时间】:2020-03-19 13:34:03
【问题描述】:

我正在尝试通过 json 格式的 AJAX 请求从 mysql 数据库中获取值。所以基本上 AJAX 获取 php 脚本。由于某种原因,它没有得到正确的值,而是显示“未定义”。pic from database

pic of result

ot5a.html

pic from html document

ot5a.js

$(document).ready(function() {
$.ajax({
    url: "ot5a.php",
    type: "GET",
    success: function(result) {
        tulostaluettelo(result);
    },
    error: function(xhr) {
        console.log(xhr.status);
    }
});

function tulostaluettelo(result) {
    var luettelo = "<table><thead><tr><th>id</th><th>Joukkue</th><th>Voitot</th><th>Tasapelit</th><th>Tappiot</th></tr></thead><tbody>";
    var i;
    for(i = 0; i < result.length; i++) {
        luettelo += "<tr><td>" + result[i].id + "</td><td>" + result[i].joukkue + "</td><td>" + result[i].voitot + "</td><td>" + result[i].tasapelit + "</td><td>" + result[i].tappiot + "</td></tr>";
    }
    luettelo += "</tbody></table>";
    $("#tulostusalue").html(luettelo);
} });

ot5a.php

php code

config.php

enter image description here

【问题讨论】:

  • 如果你在tulostaluettelo() 中执行console.log(result),输出是什么?
  • 它可以打印任何东西。刚刚 xhr 已经完成了加载 get 和文件名 ot5a.php 和 json 具有正确值的对象
  • 检查网络中 php 返回的响应。如果问题出在 php 或客户端,您将从那里得到。如果响应正确,则客户端绑定存在问题。
  • 你的意思是从配置文件中检查网络吗?如果是这样,我在哪里可以找到它们?或者我应该检查网络服务器日志文件吗?

标签: php jquery mysql ajax


【解决方案1】:

在客户端、php 和配置文件中添加断点/调试器并检查它返回的结果是什么。这样你就会知道问题到底出在哪里。 我还看到“luettelo”是一种变量类型,但您正在像数组对象一样迭代它。也改变它,看看它是否有效。

【讨论】:

  • 我尝试调试我的 js 文件,它说 $ 未定义?为什么?我已将 jquery 链接到我的 html 文件
  • 在加载 ot5a.js 文件之前是否正确加载 jquery?我的猜测是来自 CDN 的 Jquery 在您的 js 文件之前需要记录时间来加载。
  • 根据我的 Chromes Web 控制台的网络面板,加载 jquery.min 文件需要 0 毫秒(它来自缓存,我可以更改它吗?),加载我的 html 文档需要 2 毫秒。
  • 我更改为非缩小版,它会缓存所有内容。为什么?
猜你喜欢
  • 1970-01-01
  • 2018-09-15
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
相关资源
最近更新 更多