vue-cli fetch 请求

1.安装fetch

 npm install fetch-jsonp --save

2.请求数据

  methods: {
    httpGetData() {
      var api = "http://192.168.1.172:9998/test";
      fetch(api, {
        method: "GET"
      })
        .then(response => {
          return response.json();
        })
        .then(json => {
          console.log("parsed json >>>", json);
          this.lists = json.data;
        })
        .catch(ex => {
          console.log("parsing failed", ex);
        });
    },
    httpPostData() {
      var api = "http://192.168.1.172:9998/test";
      fetch(api, {
        method: "POST",
        headers: { "Content-Type": "application/json;" },
        body: JSON.stringify({ id: 1 })
      })
        .then(response => {
          return response.json();
        })
        .then(json => {
          console.log("parsed json >>>", json);
          this.lists = json.data;
        })
        .catch(ex => {
          console.log("parsing failed", ex);
        });
    }
  }

 

你可能感兴趣的:(学习)