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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀