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

     删除后的内容:

     

  • 相关阅读:
    深入浅出PyTorch——模型定义
    在MacOS中将HMCL添加到Launchpad启动台
    华清远见上海中心22071班--11.19作业
    Pytest框架和Unittest框架的区别
    比特币减半后:见证历史性暴涨吗?
    React学习笔记二
    软著申请模板
    云原生高级第一次作业
    Java SE 7 Update 17的安装配置及相关问题解决
    云计算导论(3)---分布式文件系统
  • 原文地址:https://blog.csdn.net/a1598452168YY/article/details/127755289