JQuery实现get请求json数据的三种方法:
实现php后端返回的json数据,前端利用jquery三种方法获取并且显示出来。
效果展示:
PHP代码:
<?php $arr = array('第一首诗' , '第二首诗' , '第三首诗'); echo json_encode($arr); ?>
HTML代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <ul> <li></li> <li></li> <li></li> </ul> <input type="button" name="" value="看新闻"> </body> <script type="text/javascript" src="jquery-3.4.1.js"></script> <script type="text/javascript"> //author:无陌然 blog:aidezy.com //第一种方法 $('input:button').click(function(){ $.get('13.php' , function(msg){ // alert(msg); var i = 0; $('li').each(function(){ $(this)[0].innerHTML = msg[i++]; }); } , 'json'); }); //第二种方法 $('input:button').click(function(){ $.get('13.php' , function(msg){ for(var i = 0; i <= msg.length; i++){ li[i].innerHTML = msg[i]; } } , 'json'); }); //第三种方法 $('input:button').click(function(){ $.get('13.php' , function(msg){ $('li').each(function(){ $(this)[0].innerHTML = msg.shift(); }); } , 'json'); }); </script> </html>
资源均来自第三方,谨慎下载,前往第三方网站下载 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com 解压密码:www.xkwo.com