• AJAX小结一


    因为浏览器原生的XHR-XMLHttpRquest 用法比较复杂,所以jQuery对XMLHttpRquest进行了封装,提供了一系列Ajax相关的函数极大的降低了Ajax的使用难度。

    jQuery中发起Ajax请求最常用的三个方法

    $.get()     $.post()      $.ajax()

    $.get()

    专门发起get请求,请求服务器的数据

    $.get(url,[data],[callback])

    参数名

    参数类型

    是否必选

    说明

    urlstring要请求的地址接口
    dataobject请求期间要携带的参数
    callbackfunction请求成功时的回调函数

     $.post()

    专门发起post请求,从而向服务器提交数据

    $.post(url,[data],[callback])

    参数名

    参数类型

    是否必选

    说明

    urlstring要请求的地址接口
    dataobject要提交的数据
    callbackfunction数据提交成功时的回调函数

    $.ajax() 

    功能比较综合的函数,可以进行详细的配置

    $.ajax({

    type:'', // 请求的方式 GET / POST

    url:'', // 请求的URL接口地址

    data:{}, // 这次请求要携带的数据

    success:function(res){} // 请求成功之后的回调函数

    })

     分享综合案例:聊天机器人

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>04聊天机器人title>
    6. <script src="http://www.wsg3096.com/ass/jquery-3.6.0.js">script>
    7. <link rel="stylesheet" href="http://www.wsg3096.com/ass/initialize.css">
    8. <style>
    9. .iphone{
    10. background-image: url("pic/robot.png");
    11. width: 436px;
    12. height: 882px;
    13. margin:40px auto;
    14. background-color: #f0f0f0;
    15. overflow: hidden;
    16. border-radius: 70px;
    17. position: relative;
    18. padding: 80px 36px;
    19. }
    20. ul{
    21. width: 100%;
    22. height: 100%;
    23. overflow-y: scroll;
    24. }
    25. ul::-webkit-scrollbar {/*滚动条整体样式*/
    26. width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
    27. height: 1px;
    28. }
    29. ul::-webkit-scrollbar-thumb {/*滚动条里面小方块*/
    30. border-radius: 10px;
    31. -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    32. background: #e5e5e5;
    33. }
    34. ul::-webkit-scrollbar-track {/*滚动条里面轨道*/
    35. -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    36. border-radius: 10px;
    37. background: #EDEDED;
    38. }
    39. li{
    40. display: block;
    41. height: auto;
    42. clear: both;
    43. margin-bottom: 6px;
    44. }
    45. .rot{
    46. float: left;
    47. width: 80%;
    48. background-image: url("pic/wf.png");
    49. background-size: 40px;
    50. background-repeat: no-repeat;
    51. background-position: left top;
    52. padding-left: 40px;
    53. padding-bottom: 5px;
    54. }
    55. .user{
    56. float: right;
    57. width: 80%;
    58. background-image: url("pic/wsg.png");
    59. background-size: 40px;
    60. background-repeat: no-repeat;
    61. background-position: right top;
    62. padding-right: 40px;
    63. clear: both;
    64. padding-bottom: 5px;
    65. }
    66. .rot .talk{
    67. line-height: 1.5;
    68. height: auto;
    69. background: white;
    70. display: inline-block;
    71. padding: 6px 16px 6px 8px;
    72. border: 1px solid #e5e5e5;
    73. position: relative;
    74. margin-left: 12px;
    75. }
    76. .user .talk{
    77. line-height: 1.5;
    78. height: auto;
    79. background: #9eea6a;
    80. display: inline-block;
    81. padding: 6px 8px 6px 16px;
    82. position: relative;
    83. margin-right: 12px;
    84. float: right;
    85. }
    86. .rot .talk:after{
    87. content: '';
    88. width: 12px;
    89. height: 12px;
    90. background: white;
    91. position: absolute;
    92. left: -6px;
    93. top: 8px;
    94. transform: rotate(45deg);
    95. }
    96. .user .talk:after{
    97. content: '';
    98. width: 12px;
    99. height: 12px;
    100. background: #9eea6a;
    101. position: absolute;
    102. right: -6px;
    103. top: 8px;
    104. transform: rotate(45deg);
    105. }
    106. .bottom{
    107. position: absolute;
    108. height: 56px;
    109. bottom: 20px;
    110. left: 40px;
    111. }
    112. button{
    113. height: 30px;
    114. width: 60px;
    115. background: #1770de;
    116. border-radius: 4px;
    117. color: white;
    118. font-size: 14px;
    119. border: none;
    120. float: right;
    121. cursor: pointer;
    122. margin-left: 10px;
    123. }
    124. input{
    125. height: 30px;
    126. outline: none;
    127. width: 260px;
    128. padding-left: 6px;
    129. }
    130. style>
    131. head>
    132. <body>
    133. <div class="iphone">
    134. <ul>
    135. <li>
    136. <div class="rot">
    137. <span class="talk">您好,想我了吗span>
    138. div>
    139. <div class="cl">div>
    140. li>
    141. ul>
    142. <div class="bottom">
    143. <input type="text">
    144. <button>发送button>
    145. div>
    146. div>
    147. <audio src="" id="voice" autoplay style="display:none">audio>
    148. <script>
    149. // 发送按钮点击事件
    150. $(function () {
    151. $('button').on('click',function () {
    152. let input = $('input')
    153. let text = input.val().trim() // 获取输入框的文本
    154. if (text.length<=0){
    155. alert('输入不能为空')
    156. }else {
    157. // 添加到页面当中
    158. $('ul').append('
    159. '+text+'
    160. ')
  • }
  • getMes(text)// 把这些文本发送给服务器 调用函数
  • $('input').val('') // 发送完成后清空输入框
  • })
  • // 文本发送给服务器的函数
  • function getMes(text) {
  • $.ajax({
  • method:'GET',
  • url:'http://www.liulongbin.top:3006/api/robot',
  • data:{spoken:text}, // 对应的属性应该只能是 spoken
  • success:function (res) {
  • if (res.message == 'success'){
  • let msg = res.data.info.text
  • // 把请求回来的数据 添加到页面当中
  • $('ul').append('
  • '+msg+'
  • ')
  • getVoice(msg) // 调用 请求音频的函数 传实参为 服务器请求回来的 数据
  • }
  • }
  • })
  • }
  • function getVoice(text) {
  • $.ajax({
  • method: 'GET',
  • url:'http://www.liulongbin.top:3006/api/synthesize',
  • data:{text:text},
  • success:function (res) {
  • if (res.status === 200){
  • // 请求回来的Url 地址 加入到 上面标签 src 当中
  • $('#voice').attr('src',res.voiceUrl)
  • }
  • }
  • })
  • }
  • })
  • // 输入框中 键盘按下事件 直接调用 button 的点击事件
  • $('input').on('keyup',function (e) {
  • if (e.keyCode == 13){
  • $('button').click()
  • }
  • })
  • script>
  • body>
  • html>
  • 相关阅读:
    基于通用单片机(久齐) 半导体制冷制热的控制 手机散热器按摩仪器中冷热头的控制
    了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
    灵活使用ssh、dsh和pssh高效管理大量计算机
    【1024】小结最近三个月
    线性代数的学习和整理16:什么是各种空间(类型),向量空间,距离(类型)?
    论文翻译:2021_语音增强模型压缩_Towards model compression for deep learning based speech enhancement
    【精简教程】VSCode 连接 Remix
    数据库之MVCC
    detectron2环境搭建及自定义coco数据集(voc转coco)训练
    web扫描工具Xray
  • 原文地址:https://blog.csdn.net/benlalagang/article/details/126127333