Monarch
Monarch

Reputation: 11

how to fetch data from back-end

The back-end generated the data API, and how to fetch data on the front end ps: I use react

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

when request the above url, the browser displays as follows.

{
    "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
}

Front-end: get data

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

I want to get the data of content

Upvotes: 0

Views: 3544

Answers (1)

Cat_Enthusiast
Cat_Enthusiast

Reputation: 15688

You would need to do it like this:

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

Upvotes: 1

Related Questions