如何从后端获取数据

时间:2019-05-04 07:54:39

标签: javascript reactjs

后端生成数据API,以及如何在前端获取数据 ps:我用react

URL:http://localhost:8080/api/barrages

当请求上述URL时,浏览器显示如下。

{
    "content": [
        {
            "key": 12344443434545435,
            "text": "绿色走一波",
            "time": 500,
            "fontFamily": null,
            "fontsize": null,
            "color": "#0f0",
            "video_id": null,
            "creationDateTime": "2019-04-08T13:59:51Z"
        }
    ],
    "page": 0,
    "size": 30,
    "totalElements": 1,
    "totalPages": 1,
    "last": true
}

前端:获取数据

fetch("/api/barrages", { method: 'GET' })
    .then(function (res) {
        res.json()
           .then(function (barrage) {
          console.log(barrage);
        }
        )
      })

我想获取内容数据

1 个答案:

答案 0 :(得分:1)

您需要这样做:

fetch("/api/barrages", { method: 'GET' })
    .then(function (res) {
        return res.json()
    }
    .then(function(myJson){
         console.log(JSON.stringify(myJson))
     })
   })
相关问题