亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 70
粉絲 1
評(píng)論 0
訪問(wèn)量 67322
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
jQuery留言板dom操作-ajax的get|post|jsonp跨域
葡萄枝子
原創(chuàng)
1325人瀏覽過(guò)

jQuery留言板dom操作-ajax的get|post|jsonp跨域

  1. 用常用的jQuery中的DOM操作,實(shí)現(xiàn)前面寫(xiě)的的留言本案例(todolist)
  2. 實(shí)例演示$.get,$.post,并用$.ajax再實(shí)現(xiàn)一遍,并用$.ajax實(shí)現(xiàn)jsonp中跨域請(qǐng)求
  • body 中引入 jQuery 庫(kù)
  1. <!-- 引入 jQuery 庫(kù) -->
  2. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

1. 用常用的jQuery中的DOM操作,實(shí)現(xiàn)前面寫(xiě)的的留言本案例(todolist)

  1. <!-- jQuery中的DOM操作,實(shí)現(xiàn)前面寫(xiě)的的留言本 -->
  2. <label><input type="text" id="msg" /></label>
  3. <ol class="list"></ol>
  4. <script>
  5. // 留言板追加數(shù)據(jù)
  6. $('#msg').on('keydown', function (ev) {
  7. // 非空回車(chē)時(shí)
  8. if ($(this).val().length > 0) {
  9. if (ev.key == 'Enter') {
  10. let msg = `<li>${$(this).val()} [<a href="javascript:;" onclick="$(this).parent().remove();">刪除</a>]</li>`;
  11. // 新添加的留言在前
  12. $('ol.list').prepend(msg);
  13. // 清空上一條留言
  14. $(this).val(null);
  15. }
  16. }
  17. });
  18. </script>
  • 添加3條留言

留言本

  • 刪除中間留言

刪除留言

2. 實(shí)例演示$.get,$.post,并用$.ajax再實(shí)現(xiàn)一遍,并用$.ajax實(shí)現(xiàn)jsonp中跨域請(qǐng)求

2.1 實(shí)例演示$.get,$.post,并用$.ajax再實(shí)現(xiàn)一遍

  • body 中添加html和js
  1. <!-- 實(shí)例演示$.get,$.post,并用$.ajax再實(shí)現(xiàn)一遍 -->
  2. <select name="select" id="select">
  3. <option value="1">1</option>
  4. <option value="2">2</option>
  5. <option value="3">3</option>
  6. <option value="4">4</option>
  7. </select>
  8. <hr />
  9. <!-- $.get() 請(qǐng)求方法 -->
  10. <button class="get">$.get()</button>
  11. <!-- $.post() 請(qǐng)求方法 -->
  12. <button class="post">$.post()</button>
  13. <!-- $.ajax() - get 請(qǐng)求 -->
  14. <button class="ajax-get">$.ajax() - get</button>
  15. <!-- $.ajax() - post 請(qǐng)求 -->
  16. <button class="ajax-post">$.ajax() - post</button>
  17. <script>
  18. $("button").click(function (ev) {
  19. const select = $('#select').val();
  20. const url = 'ajax/users.php';
  21. switch ($(this).attr("class")) {
  22. // $.get() 請(qǐng)求方法
  23. case 'get':
  24. $.get(url, { id: select }, function (data) {
  25. // 防止重復(fù)添加
  26. if (!$(ev.target).next('p').length)
  27. $(ev.target).after("<p></p>").next("p").html(data);
  28. });
  29. break;
  30. // $.post() 請(qǐng)求方法
  31. case 'post':
  32. $.post(url, { id: select }, function (data) {
  33. if (!$(ev.target).next('p').length)
  34. $(ev.target).after("<p></p>").next("p").html(data);
  35. });
  36. break;
  37. // $.ajax() - get 請(qǐng)求
  38. case 'ajax-get':
  39. $.ajax({
  40. type: 'get',
  41. url: url,
  42. data: { id: select },
  43. // dataType: 'html',
  44. success: function (data) {
  45. if (!$(ev.target).next('p').length)
  46. $(ev.target).after("<p></p>").next("p").html(data);
  47. }
  48. });
  49. break;
  50. // $.ajax() - post 請(qǐng)求
  51. case 'ajax-post':
  52. $.ajax({
  53. type: 'post',
  54. url: url,
  55. data: { id: select },
  56. // dataType: 'html',
  57. success: function (data) {
  58. if (!$(ev.target).next('p').length)
  59. $(ev.target).after("<p></p>").next("p").html(data);
  60. }
  61. });
  62. }
  63. });
  64. </script>
  • 請(qǐng)求的 ajax/users.php 是默認(rèn)的
  1. <?php
  2. // 二維數(shù)組來(lái)模擬數(shù)據(jù)表的查詢結(jié)果
  3. $users = [
  4. ['id' => 1, 'name' => '天蓬大人', 'age' => 66],
  5. ['id' => 2, 'name' => '滅絕師妹', 'age' => 55],
  6. ['id' => 3, 'name' => '西門(mén)老妖', 'age' => 44],
  7. ];
  8. // $_REQUEST: 相當(dāng)于 $_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(輸出模板, 數(shù)組表示的參數(shù))
  13. vprintf('%s: %s %s歲',$user);
  14. // 以下語(yǔ)句配合$.getJSON()調(diào)用,其它請(qǐng)求時(shí)請(qǐng)注釋掉
  15. // echo json_encode($user);
  16. }
  17. }
  18. } else {
  19. die('<span style="color:red">沒(méi)找到</span>');
  20. }
  • 下拉選項(xiàng)依次選 1~4,依次點(diǎn)擊四個(gè)按鈕,由于 users 沒(méi) id=4 的,最后一個(gè)按鈕請(qǐng)求的結(jié)果是未找到

ajax請(qǐng)求

2.2 用$.ajax實(shí)現(xiàn)jsonp中跨域請(qǐng)求

  • body 中追加html和js
  1. <!-- 用$.ajax實(shí)現(xiàn)jsonp中跨域請(qǐng)求 -->
  2. <p>
  3. <label for="jsonp">jsonp跨域</label>
  4. <select name="jsonp" id="jsonp">
  5. <option value="1">1</option>
  6. <option value="2">2</option>
  7. <option value="3">3</option>
  8. </select>
  9. </p>
  10. <script>
  11. // jsonp 跨域請(qǐng)求動(dòng)態(tài) id , 回調(diào)函數(shù)名為 show 告訴服務(wù)器端
  12. $("#jsonp").change(function () {
  13. let id = $(this).val();
  14. $.ajax({
  15. type: 'get',
  16. url: 'http://wordpress/world/test.php?id=' + id + '&jsonp=show',
  17. dataType: 'jsonp',
  18. jsonpCallback: "show",
  19. });
  20. });
  21. // 處理響應(yīng)結(jié)果的回調(diào)函數(shù)
  22. function show(res) {
  23. // 將內(nèi)容追加到頁(yè)面中
  24. $("#jsonp").after("<p></p>").next().html(`${res.name} (${res.email})`);
  25. }
  26. </script>
  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. // 獲取回調(diào)名稱
  4. $callback = $_GET['jsonp'];
  5. $id = $_GET['id'];
  6. // 模擬接口數(shù)據(jù)
  7. $users = [
  8. 0=>'{"name":"朱老師", "email":"peter@php.cn"}',
  9. 1=>'{"name":"西門(mén)老師", "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. // 動(dòng)態(tài)生成handle()的調(diào)用
  17. echo $callback . '(' . $user . ')';
  • html文檔 jsonp跨域,依次點(diǎn)擊3,2,1改變傳遞的id值,獲得跨域結(jié)果圖

jsonp跨域

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)