0
I have the following code to fetch data from an API via POST, I get the result from JSON (as I can write it to the console. However, I cannot recur the values to fill the Vue element array.
// Get Pipelines
const getPipelines = new Vue({
el: '#top-pipelines',
data: {
pipelines: []
},
mounted() {
axios({
method: 'post',
url: 'http:..xpto.../getpipelines',
data: {
orgId: "ORGIDXPTO",
pipelineId: "19",
dateFrom: "2017-11-01",
dateTo: "2017-11-31"
}
})
.then(function (response) {
console.log(response);
this.pipelines = response.json;
})
}
});
tried also "this.pipelines = Sponse.data;"
Important to say that my reply JSON does not have a header row before the object array (see below).
[
{
"id_pipeline": "NOME_PIPELINE_XPTO",
"id_org": "ORGIDXPTO",
"name": "Nome Pipeline",
"active": "1"
},
{
"id_pipeline": "NOME_PIPELINE_XPTO",
"id_org": "ORGIDXPTO",
"name": "Nome Pipeline",
"active": "1"
},
{
"id_pipeline": "NOME_PIPELINE_XPTO",
"id_org": "ORGIDXPTO",
"name": "Nome Pipeline",
"active": "1"
},
{
"id_pipeline": "NOME_PIPELINE_XPTO",
"id_org": "ORGIDXPTO",
"name": "Nome Pipeline",
"active": "1"
},
{
"id_pipeline": "NOME_PIPELINE_XPTO",
"id_org": "ORGIDXPTO",
"name": "Nome Pipeline",
"active": "1"
}
]
Any hint?
Since your answer is an array you are trying to access
response.json
asjson
being a property ofresponse
it should return an error for trying to access a property named after an array. Since in Axios you do not define that the response to be received is a json you receive text and not JSON, so try to usethis.pipelines = JSON.parse(response);
to deserialize– Rômulo Gabriel Rodrigues
Hi Huxley, you tested @Romulogabrielrodrigues' suggestion?
– Sergio
Thanks for the suggestion Romulus, I tried to use and also did not work. I was able to fix setting the headers
headers: {
 'Content-Type': 'application/json',
 'Accept': 'application/json'
 }
and I also recovered the Restsponse thus.then(response => {
 this.pipelines = response.data
 })
Hug– Huxley Dias
What’s on the console if you do
console.log(typeof response, response);
?– Sergio