json-server----模拟后端接口

1.全局安装
cnpm install json-server -g
2.建立一个文件夹dataserver,建立文件data.json----------文件夹和文件都是自己定义的
在文件夹中打开终端,
json-server data.json  运行,生成接口
 
 
json-server实现数据的增删改查
 
//------------------------json-server------------
  //模拟的接口只支持get请求,不支持post
  //增:增数据时用post,这里的post不是请求的方式,而是json-server增数据的方式
  created() {
    axios({
      method: "post",
      url: "http://localhost:3000/list",
      data: {
          //增的数据
          username:"王五",
          age:30
 
      }
    }).then((data)=>{
        //成功的回调函数,返回的是增加的数据
        console.log(data);
    });
 
 
    put实现改,只能根据id进行修改,put会将数据里面的所有数据都覆盖, patch:只会修改数据中的某一个
     axios({
      method: "put",
      url: "http://localhost:3000/list/1",
      data: {
          //根据url中的id进行修改
          username:"赵六",
          age:30
 
      }
    }).then((data)=>{
        //成功的回调函数,返回的是增加的数据
        console.log(data);
    });
 
    查,,get实现查
     axios({
      method: "get",
    //   查询指定id的内容
    //   url: "http://localhost:3000/list/1",
 
    //查询所有的内容
    //   url: "http://localhost:3000/list",
 
    //条件查询
    //   url: "http://localhost:3000/list?age_gte=30",
 
    // 模糊查询
      url: "http://localhost:3000/list?q=张",
 
      
    }).then((data)=>{
        //成功的回调函数,返回的是查到的数据
        console.log(data);
    });
 
 
    //删delete
    axios({
        method:"delete",
        //根据id进行删除
        url: "http://localhost:3000/list/2",
        
    }).then((data)=>{
        console.log(data);
    })
 
 
 
 
 
  }
 
 

 

 
posted @ 2019-02-27 17:34  SRH啦  阅读(508)  评论(0编辑  收藏  举报