axios

json-server

安装:

  npm install -g json-server

运行:

  json-server --watch db.json

axios函数

  method:get 获取、post 添加、put 更新、delete 删除

    //   获取 get
      btns[0].addEventListener("click", () => {
        axios({
          method: "GET",
          url: "http://localhost:3000/posts/2",
        }).then((response) => {
          console.log(response);
        });
      });
      //   添加 post
      btns[1].addEventListener("click", () => {
        axios({
          method: "POST",
          url: "http://localhost:3000/posts",
          data: {
            title: "时间控制大师",
            auther: "rql",
          },
        }).then((response) => {
          console.log(response);
          console.log("请求成功");
        });
      });
      //   更新 put
      btns[2].addEventListener("click", () => {
        axios({
          method: "PUT",
          url: "http://localhost:3000/posts/3",
          data: {
            title: "时间控制大师",
            auther: "任青龙",
          },
        }).then((response) => {
          console.log(response);
          console.log("请求成功");
        });
      });

      //   删除 delete
      btns[3].addEventListener("click", () => {
        axios({
          method: "DELETE",
          url: "http://localhost:3000/posts/3",
        }).then((response) => {
          console.log(response);
          console.log("请求成功");
        });
      });

 

posted @ 2022-09-10 08:28  二王戏木  阅读(5)  评论(0编辑  收藏  举报