PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > jQuery实现留言本、$.get,$.post,$.ajax实例演示、$.ajax实现jsonp跨域请求

jQuery实现留言本、$.get,$.post,$.ajax实例演示、$.ajax实现jsonp跨域请求

吳
原创
2021年01月14日 16:57:28 576浏览

1.jquery实现留言本

删除前

删除后

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>留言本</title>
  6. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <label><input type="text" id="msg" /></label>
  10. <ol class="list"></ol>
  11. <script>
  12. $("#msg").on("keydown", function (ev) {
  13. // 非空时回车
  14. if ($(this).val().length > 0) {
  15. if (ev.key === "Enter") {
  16. let msg = `<li>${$(this).val()} [<a href="javascript:;" onclick="$(this).parent().remove();">删除</a>]</li>`;
  17. // 新添加的留言在前
  18. $("ol.list").prepend(msg);
  19. // 清空上一条留言
  20. $(this).val(null);
  21. }
  22. }
  23. });
  24. </script>
  25. </body>
  26. </html>

2.$.get,$.post,$.ajax、$.ajax实现jsonp跨域请求

  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 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议