博客列表 >jquery进阶实例:jQuery留言板dom操作|jQuery中ajax的get|post|jsonp跨域

jquery进阶实例:jQuery留言板dom操作|jQuery中ajax的get|post|jsonp跨域

幸福敲门的博客
幸福敲门的博客原创
2021年01月17日 01:04:271246浏览

jquery进阶实例:jQuery留言板dom操作|jQuery中ajax的get|post|jsonp跨域

用常用的jQuery中的DOM操作,实现前面写的的留言本案例(todolist)
实例演示$.get,$.post,并用$.ajax再实现一遍,并用$.ajax实现jsonp中跨域请求

一、jQuery留言板dom操作

  1. <body>
  2. <h2>我要留言</h2>
  3. <!-- 引入jQyery库 -->
  4. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  5. <label><input type="text" id="wyly" /></label>
  6. <ol class="list"></ol>
  7. <script>
  8. //留言板添加数据
  9. $('#wyly').on('keydown', function (ev) {
  10. // 非空回车判断
  11. if ($(this).val().length > 0) {
  12. if (ev.key == 'Enter') {
  13. let wyly = `<li>${$(this).val()} [<a href="javascript:;" onclick="$(this).parent().remove();">删除</a>]</li>`;
  14. // 新添加的留言在前
  15. $('ol.list').prepend(wyly);
  16. // 清空上一条留言
  17. $(this).val(null);
  18. }
  19. }
  20. });
  21. </script>
  22. </body>

图示:
jQuery留言板dom操作
jQuery留言板dom操作

二、jQuery中ajax的get|post|jsonp跨域

2.1实例演示$.get,$.post,并用$.ajax再实现一遍

  1. <button class="get">1.$.get():请求数据</button>
  2. <button class="post">2.$.post():请求数据</button>
  3. <button class="ajax">3.$.ajax():请求数据</button>
  4. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  5. <script>
  6. // 1.$.get(请求url,查询参数,成功回调)
  7. $(".get").click(function (ev) {
  8. $.get("users.php", { id: 2 }, function (data) {
  9. $(ev.target).after("<div></div>").next().html(data);
  10. });
  11. });
  12. // 2. post()
  13. $(".post").click(function (ev) {
  14. $.post("users.php", { id: 3 }, function (data) {
  15. // console.log(data);
  16. $(ev.target).after("<div></div>").next().html(data);
  17. });
  18. });
  19. // 3. $.ajax()
  20. $(".ajax").click(function (ev) {
  21. $.ajax({
  22. type: 'get',
  23. url: 'users.php',
  24. data: { id: 1 },
  25. dataType: 'html',
  26. success: function (data) {
  27. $("button:last-of-type").after("<div>").next().html(data);
  28. }
  29. })
  30. })
  31. </script>

$.get,$.post,$.ajax、$.ajax请求php代码

  1. <?php
  2. // 二维数组来模拟数据表的查询结果
  3. $users = [
  4. ['id' => 1, 'name' => '天蓬大人', 'age' => 66],
  5. ['id' => 2, 'name' => '灭绝师妹', 'age' => 55],
  6. ['id' => 3, 'name' => '西门老妖', 'age' => 44],
  7. ];
  8. // $_REQUEST: 相当于 $_GET + $_POST + $_COOKIE 三合一
  9. if (in_array($_REQUEST['id'], array_column($users, 'id'))) {
  10. foreach ($users as $user) {
  11. if ($user['id'] == $_REQUEST['id']) {
  12. // vprintf(输出模板, 数组表示的参数)
  13. vprintf('%s: %s %s岁',$user);
  14. // 以下语句配合$.getJSON()调用,其它请求时请注释掉
  15. // echo json_encode($user);
  16. }
  17. }
  18. } else {
  19. die('<span style="color:red">没找到</span>');
  20. }

$.ajax实现jsonp跨域请求

  1. <button class="jsonp">$.ajax():jsonp:跨域请求数据</button>
  2. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  3. <script>
  4. $(".jsonp").click(function (ev) {
  5. $.ajax({
  6. type: "get",
  7. url: "http://world.io/test.php?id=1&jsonp=?",
  8. dataType: "jsonp",
  9. // 告诉跨域访问的服务器需要返回的函数名称
  10. // jsonpCallback:"show",
  11. success: function (data) {
  12. console.log(data);
  13. $("button:last-of-type").after("<div>").next().html(`${data.name} 邮箱:${data.email}`);
  14. },
  15. });
  16. });
  17. </script>

跨域请求
$.ajax jsonp跨域请求PHP代码

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. // 获取回调名称
  4. $callback = $_GET['jsonp'];
  5. $id = $_GET['id'];
  6. // 模拟接口数据
  7. $users = [
  8. 0=>'{"name":"朱老师", "email":"peter@php.cn"}',
  9. 1=>'{"name":"西门老师", "email":"xm@php.cn"}',
  10. 2=>'{"name":"猪哥", "email":"pig@php.cn"}'
  11. ];
  12. if (array_key_exists(($id-1), $users)) {
  13. $user = $users[$id-1];
  14. }
  15. // echo $user;
  16. // 动态生成handle()的调用
  17. echo $callback . '(' . $user . ')';
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议