likes
comments
collection
share

jQuery-Ajax请求Json数据并加载在前端页面,附视频教程讲解!

作者站长头像
站长
· 阅读数 49

Ajax技术应用广泛,这种异步加载技术,无需刷新网页即可更新网站内容,全局或者局部均可,所以大家应该学会这种技巧,把技术用上来。

创建demo.json文件,用来做数据源:

{
    "title":"Segmentfault",
    "url":"https://segmentfault.com"
}

创建index.html文件,前端页面并加载数据

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>jQuery - Ajax - Json</title>
    <script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
        $(function(){
            $("button").click(function(){
                $.ajax({
                    type:"GET",
                    url:"demo.json",
                    dataType:"json",
                    success:function(data){
                        $("#title").text(data.title);
                        $("#url").text(data.url);
                    }
                })
            })
        })
    </script>
</head>
<body>
    <button name="button" type="button">加载数据</button>
    <h2>title:<span id="title"></span></h2>
    <h2>url:<span id="url"></span></h2>
</body>
</html>

页面中需要引入jQuery库视频教程:https://pan.baidu.com/s/1vpfT...