• ajax中的和后端交互的put、patch、delete请求


    1、向后端发送一个put请求,请求修改数据:

    • 重点:修改谁、修改成什么:
    • 修改谁:修改user下第一个:
    xhr.open("PUT","http://localhost:3000/user/1")

    user内容:

    • 修改成什么:修改成“username=hhh&password=575”
    xhr.send(`username=hhh&password=575`)

    完整代码:(只复制了body里面的代码)

    1. <button id="myget">getbutton>
    2. <button id="mypost">postbutton>
    3. <button id="myput">putbutton>
    4. <button id="mypatch">patchbutton>
    5. <button id="mydelete">deletebutton>
    6. <script>
    7. myput.onclick = function(){
    8. var xhr = new XMLHttpRequest()
    9. xhr.open("PUT","http://localhost:3000/user/1")
    10. xhr.onload = function(){
    11. if(/^2\d{2}$/.test(xhr.status)){
    12. console.log(JSON.parse(xhr.responseText))
    13. }else if(xhr.status === 404){
    14. console.log("页面加载错误")
    15. }
    16. }
    17. //表单格式
    18. xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    19. xhr.send(`username=hhh&password=575`)
    20. //json格式
    21. // xhr.setRequestHeader("Content-Type","application/json")
    22. // xhr.send(JSON.stringify({
    23. // username:"hhh",
    24. // password:"575"
    25. // }))
    26. }
    27. script>

     结果:

    json原来的内容:

    json文件内容的变化:

    user内容变化:

     

    2、向后端发送一个patch请求,请求修改部分数据:

    • 和put的区别是,put的全部修改,patch是部分修改;
    • 要修改谁:修改user中id为2的
    xhr.open("PATCH","http://localhost:3000/user/2")
    • 修改成什么内容:修改username的值为“dd”
    xhr.send(`username=dd`)
    • 完整代码:
    1. <button id="myget">getbutton>
    2. <button id="mypost">postbutton>
    3. <button id="myput">putbutton>
    4. <button id="mypatch">patchbutton>
    5. <button id="mydelete">deletebutton>
    6. <script>
    7. myput.onclick = function(){
    8. var xhr = new XMLHttpRequest()
    9. xhr.open("PATCH","http://localhost:3000/user/2")
    10. xhr.onload = function(){
    11. if(/^2\d{2}$/.test(xhr.status)){
    12. console.log(JSON.parse(xhr.responseText))
    13. }else if(xhr.status === 404){
    14. console.log("页面加载错误")
    15. }
    16. }
    17. //表单格式
    18. xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
    19. xhr.send(`username=dd`)
    20. }
    21. script>

    结果:

    user本来的内容:

    修改后的内容:

     

    3、delete请求方式:

    • 要删什么:删user下的第2条数据
    xhr.open("DELETE","http://localhost:3000/user/2")
    • 完整代码: 
    1. <button id="myget">getbutton>
    2. <button id="mypost">postbutton>
    3. <button id="myput">putbutton>
    4. <button id="mypatch">patchbutton>
    5. <button id="mydelete">deletebutton>
    6. <script>
    7. myput.onclick = function(){
    8. var xhr = new XMLHttpRequest()
    9. xhr.open("DELETE","http://localhost:3000/user/2")
    10. xhr.onload = function(){
    11. if(/^2\d{2}$/.test(xhr.status)){
    12. console.log(JSON.parse(xhr.responseText))
    13. }else if(xhr.status === 404){
    14. console.log("页面加载错误")
    15. }
    16. }
    17. xhr.send()
    18. }
    19. script>

    结果:

     user本来的内容:

     删除后的内容:

     

  • 相关阅读:
    第二十一章《万年历》第2节:系统功能实现
    【Spring Boot】实现全局异常处理
    高通平台Android 蓝牙调试和配置手册-- HCI Transport Failures: Command Timeout
    Java基础知识第七讲:Java异常处理与日志打印
    前端知识案例105-javascript基础语法-捕获多个异常
    用Python和OpenGL实现体光线投射算法(Win11)
    CPT205 Lab1 Code Collection
    多御安全浏览器更新了这些设置,还是你熟悉的吗?
    100+大屏模板免费领!葡萄城BI行业应用方案重磅发布!
    HTML5期末大作业:基于HTML+CSS+JavaScript校园文化企业网站模板【学生网页设计作业源码】
  • 原文地址:https://blog.csdn.net/a1598452168YY/article/details/127755289