• 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本来的内容:

     删除后的内容:

     

  • 相关阅读:
    day28--JS(同步异步代码,回调函数地狱,promise链式调用,async函数和await,事件循环,宏任务与微任务)
    2004NOIP普及组真题 4. 火星人
    Hive——DDL(Data Definition Language)数据定义语句用法详解
    深入理解计算机系统——第四章 Processor Architecture
    猴子也能学会的jQuery第七期——jQuery动画(上)
    右值引用、std::move()和移动构造 的理解
    ES日期时间格式化参数集锦
    RMAN备份数据库_重启RMAN备份
    领域驱动设计:领域模型与代码模型的一致性
    【第2章 Node.js基础】2.4 Node.js 全局对象(二) process 对象
  • 原文地址:https://blog.csdn.net/a1598452168YY/article/details/127755289