前端实现AJAX收取数据

[
    {
        "title": "Eternal Summer",
        "director": "Jia Zhangke",
        "release_date": "2024-06-01",
        "box_office": "$12,345,678",
        "image_url": "https://example.com/images/movies/eternal_summer_poster.jpg"
    },
    {
        "title": "Galactic Odyssey",
        "director": "Christopher Nolan",
        "release_date": "2024-05-25",
        "box_office": "$9,876,543",
        "image_url": "https://example.com/images/movies/galactic_odyssey_poster.png"
    },
    {
        "title": "The Dream Reawakens",
        "director": "Ang Lee",
        "release_date": "2024-05-18",
        "box_office": "$7,777,777",
        "image_url": "https://example.com/images/movies/dream_reawakens_poster.jpeg"
    },
    {
        "title": "Mystery of the Lost City",
        "director": "Ridley Scott",
        "release_date": "2024-05-11",
        "box_office": "$5,666,666",
        "image_url": "https://example.com/images/movies/lost_city_poster.gif"
    },
    {
        "title": "Chronicles of a Time Traveler",
        "director": "Denis Villeneuve",
        "release_date": "2024-05-04",
        "box_office": "$4,545,454",
        "image_url": "https://example.com/images/movies/time_traveler_poster.svg"
    }
]

上面是json数据源 

具体代码是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        ul li{
            list-style: none;
        }
        #app{
            width: 1200px;
            margin: 10px auto;
        }
        /* 弹出盒子的样式 */
        #app ul{
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
        }
        #app ul li{
            width: 150px;
            height:220px;
            border: 1px solid #ccc;
            text-align: center;
        }
        #app ul li img{
            width: 100%;
            height:150px;
        }
        #app ul li a{
            font-size: 12px;
            text-decoration: none;
            color: black;
        }
        #app ul li a:hover{
            color: coral;
        }
    </style>
</head>
<body>
    <div id="app">
        <ul>
            <li><img src="" alt="">
                <p>
                    <a href="#">
                    <span>Eternal Summer</span></p>
                <span>$12,345,678</span>
                 </a>
                 
                 
            <p>
                <a href="#">
                <span>Jia Zhangke</span>
                <span>2024-06-01</span>
                </a>


            </p>
         </li>
            
        </ul>
    </div>

<script>
        //创建对象
        var xhr = new XMLHttpRequest();
       //打开对象
        xhr.open('get','movie.json')
        //监听
        xhr.send(null)
        xhr.onreadystatechange = function(){
            if(xhr.status==200&&xhr.readyState==4){
                var response = xhr.responseText;  //取服务端返回的数据
                console.log(response);
                var  data = JSON.parse(response)
                console.log(data);
                //遍历
                var mstr = ""
                data.forEach((item,index)=>{
                    console.log(item.title);
                    mstr+= `<li><img src="${item.image_url}" alt="">
                <p>
                    <a href="#">
                    <span>${item.title}</span></p>
                <span>${item.box_office}</span>
                 </a>
                 
                 
            <p>
                <a href="#">
                <span>${item.director}</span>
                <span>${item.release_date}</span>
                </a>

            </p>
         </li>`
                })
                document.querySelector('#app ul').innerHTML = mstr
                
            }
        }
        //同步操作
        // console.log(1);
        // setTimeout(function(){
        //     console.log(4);
        // },1000)

        // setTimeout(function(){
        //     console.log(5);
        // },1000)
        // console.log(2);
        // console.log(3);


        
    </script>
</body>
</html>

最终实现效果:

  • 16
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值