666游戏网
您的当前位置:首页jQuery-Ajax请求Json数据并加载在前端页面

jQuery-Ajax请求Json数据并加载在前端页面

来源:666游戏网


这篇文章主要介绍了关于jQuery-Ajax请求Json数据并加载在前端页面,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

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库

显示全文