weixin_33720956 2020-04-14 14:51 采纳率: 0%
浏览 36

使用AJAX显示JSON数据

i am making a project using xampp, and i MUST use AJAX to show the data from a JSON file that i create. My problem is that i don't understand how to use AJAX to show JSON data using JQUERY/JS. I tried this 2 script watching tutorials on youtube/google, but no one working. Can someone tell me how can i fix the code or how can i use AJAX to show my local JSON file? I have made 2 attempts using 2 different methods I have no console errors but the website show nothing.

Method 1

$(document).ready(function(){
    $.getJSON("../json/output.json", function(data){
        var project_data = '';
        $.each(data, function(key, value){
            project_data += '<tr>';
            project_data += '<td>' + value.autore + '</td>';
            project_data += '<td>' + value.title + '</td>';
            project_data += '<td>' + value.category + '</td>';
            project_data += '<td>' + value.descr + '</td>';
            project_data += '<td>' + value.data + '</td>';
            project_data += '<td>' + value.budget + '</td>';
            project_data += '<td>' + value.flag + '</td>';
            project_data += '</tr>'
        });
        $('#progetti').append(project_data);
    });
});

Method 2

$(document).ready(function(){
    var ourRequest = new XMLHttpRequest();
    ourRequest.open('GET', '../json/output.json');
    var containerPrj = document.getElementById("container");
    ourRequest.onload = function() {
        var ourData = JSON.parse(ourRequest.responseText);
        showData(ourData);
    };
});

function showData(data){
    var htmlString = "";
    for(i = 0; i < data.length; i++){
        htmlString += '<p>' + data[i].id + data[i].autore +  data[i].title + '</p>';
    }
    containerPrj.insertAdjacentHTML('beforeend', htmlString);
}

here is my html if you need

<!DOCTYPE HTML>
<html>
    <head>
        <?php 
            include ("top.php");
            include ("common.php");
            ensure_logged_in();
        ?>
        <link href="style/project.css" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="js/script4.js"></script>
    </head>
    <body>
        <?php include ("nav.php");?>
        <?php get_projects();?>
        <table id = "progetti">
            <tr>
                <th>autore</th>
                <th>titolo</th>
                <th>categoria</th>
                <th>descrizione</th>
                <th>data</th>
                <th>budget</th>
                <th>disponibile</th>
            </tr>
        </table>

        <div id = "container"></div>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • weixin_33716154 2020-04-14 16:03
    关注

    Is your JSON file actually accessible via the path you specified? Seems like the file does not get found at all. Make sure the file can be accessed via the path specified.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog