使用fetch请求数据

第一种写法:

  getData() {
    fetch('/api/list_all').then(res => {
      return res.json()
    })
    .then(res => {
      if(res.code === 200) {

      }
    })
    .catch(err => {
      console.log(err)
    })
  }

第二种写法:

  async getData() {
    try {
      const result = await fetch('/api/list_all')
      const res = await result.json()
      if (res.code === 200) {

      }
    } catch (error) {
      console.log(error)
    }
  }

 

posted @ 2021-04-01 10:14  徐同保  阅读(126)  评论(0编辑  收藏  举报