Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read stream+json responses from Vuejs?

I use axios http client in vuejs application and this code:

axios.get('http://localhost:8081/pets')
    .then(response => {
      this.pets = response.data;
    })

If server return simple "application/json" conten, then all fine. But i want read "application/stream+json" for each row separately.

For example:

axios.get('http://localhost:8081/pets')
    .then(response => {
      this.pets.push(response.data)
    })

But this code (as expected) does not work.

like image 507
mgramin Avatar asked Oct 14 '18 13:10

mgramin


People also ask

What is Axios vue?

Axios is an Excellent HTTP library that executes on both client and a server, which makes an API request, does the task to produce the result and specifies easier concepts to read and debug. Vue. js is the front-end JavaScript Framework to build tools and libraries. The Axios works well in the platform like node.

How do I access JSON data in vue?

To access external JSON file objects in Vue. js app, we can import the JSON with import and then use that as the value of a reactive property. to import the json array from the ./json/data.

How do I get data from vue API?

Vue Fetch data from API example To get the actual JSON body of the response, we use response. json() method. We can also access metadata such as headers , status , statusText , type , url from the Response object. The response Promise does not reject on HTTP errors (for example: 404 , 500 ).


1 Answers

I solved this problem with SSE:

let es = new EventSource('http://localhost:8081/pets');
es.addEventListener('message', event => {
    let data = JSON.parse(event.data);
    this.pets.push(data);
}, false);
like image 155
mgramin Avatar answered Oct 05 '22 18:10

mgramin