how to fetch data from back-end

119
May 04, 2019, at 5:10 PM

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

Answer 1

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))
     })
   })
READ ALSO
How to change the code so the upload progress is being displayed for every file when uploading multiple files at once?

How to change the code so the upload progress is being displayed for every file when uploading multiple files at once?

I have setup an uploader, which can upload multiple files at once and currently shows you the progress of file being uploadI would like to show the progress of every file

82
is there a way that allows creating a “post” method when some condition becomes true? [on hold]

is there a way that allows creating a “post” method when some condition becomes true? [on hold]

i am trying to create post request and passing some values like name,password, Head (checkbox) and email in thatI want to another request based on some condition which is,

103
Is dynamic schema possible with mongodb and nodejs? [duplicate]

Is dynamic schema possible with mongodb and nodejs? [duplicate]

This question already has an answer here:

107
How to remove specific item from JSON object correctly in REACT js?

How to remove specific item from JSON object correctly in REACT js?

In REACT JS, I have a JSON object in state component "myrecords" where items are grouped by their Company Names and it has following format:

95