博客列表 >JQuery实现AJAX的无刷新操作和跨域请求

JQuery实现AJAX的无刷新操作和跨域请求

王娇
王娇原创
2020年06月04日 14:45:51677浏览

学习总结

1.利用jquery实现无刷新操作可以大大精简代码,使用非常方便

2.利用jquery实现跨域请求,只需将ajax()方法中的dataType=jsonp,然后设置回调即可,非常方便

1.JQuery实现无刷新操作index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>使用JQuery实现ajax中的get请求</title>
  7. <script src="js/jquery-3.5.1.js"></script>
  8. <style>
  9. body{
  10. display: grid;
  11. gap: 5px;
  12. }
  13. p{
  14. background-color: lightseagreen;
  15. border-radius: 10px;
  16. padding: 10px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <h1>使用JQuery-ajax-GET-实现无刷新操作</h1>
  22. <textarea name="content" id="content" cols="30" rows="10" placeholder="输入评论内容"></textarea>
  23. <button type="button">添加</button>
  24. <div>
  25. <span>发送人:</span><span id="sendPerson"></span>
  26. <p id="showContent" style="height: 100px;"></p>
  27. <span>发送日期:</span><span id="sendDate"></span>
  28. </div>
  29. </form>
  30. <script>
  31. var btn=$('button');
  32. btn.click(function(){
  33. $.ajax({
  34. type:'GET', //异步请求的请求类型
  35. url:'ajaxGet.php',//异步请求的请求地址
  36. data:{//异步请求的数据
  37. content:$('#content').val()
  38. },
  39. dataType:'json',//异步请求返回值的类型
  40. success:function(data){//异步请求成功后的回调函数,data传回来的json数据jquery已经解析好了,直接使用即可
  41. $('#sendPerson').text(data['person']);
  42. $('#showContent').text(data['content']);
  43. $('#sendDate').text(data['date']);
  44. $('#content').val("");//设置内容区域为空
  45. $('#content').focus();//把焦点设置在内容区域输入
  46. }
  47. });
  48. });
  49. </script>
  50. </body>
  51. </html>

2.JQuery请求的数据文件ajaxGet.php

  1. <?php
  2. $add['person'] = 'angle';
  3. $add['date'] = date('Y-m-d');
  4. $add['content'] = $_GET['content'];
  5. $jsonPerson = json_encode($add);
  6. echo $jsonPerson;
  7. ?>

3.JQuery实现跨域请求jsonp.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <script src="js/jquery-3.5.1.js"></script>
  7. <title>JQuery实现跨域请求</title>
  8. </head>
  9. <body>
  10. <h2>JQuery实现跨域请求</h2>
  11. <div></div>
  12. <div></div>
  13. </body>
  14. <script>
  15. $.ajax({
  16. type: "GET", //异步请求的请求类型
  17. url: "http://58city.com/JQuerydata.php", //异步请求的请求地址
  18. // data: {
  19. // //异步请求的数据
  20. // content: $("#content").val(),
  21. // },
  22. dataType: "jsonp", //异步请求返回值的类型
  23. jsonpCallback: "handle",
  24. });
  25. function handle(data) {
  26. console.log(data);
  27. //跨域请求回来的json数据jquery也已经解析完成,直接使用就好
  28. $("div:first-of-type").text("姓名:" + data["name"]);
  29. $("div:last-of-type").text("年龄:" + data["age"]);
  30. }
  31. </script>
  32. </html>

4.跨域请求的数据http://58city.com/JQuerydata.php

  1. <?php
  2. $stu['name'] = 'Eric';
  3. $stu['age'] =6;
  4. $jsonStr = json_encode($stu);
  5. //handle是请求数据的回调函数名,参数是一个json字符串
  6. echo "handle($jsonStr)";
  7. ?>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议