Donnie Donnie - 2 months ago 150
Javascript Question

vue-resource returning a PromiseObj

How to I access the response data within an Ajax call? If I log

response.text()
it shows me a
PromiseObj
.

Console

PromiseObj
context: undefined
promise: Promise {status: "resolved", result: ")]}',↵{\"Result\":\"SUCCESS\",\"Data\":{\"mode\":\"DEV\"}}"}


Code

this.$http.post(endpoint, data, []).then((response) => {
console.log(response.status);
console.log(response.text());
}, (response) => {
console.log(response.status);
console.log(response.json());
});

Answer

Promise result values are supposed to be consumed using the then method:

response.text().then(console.log)

You can simplify your code by returning that promise and chaining onto it:

this.$http.post(endpoint, data, []).then(response => {
    console.log(response.status);
    return response.text();
}, response => {
    console.log(response.status);
    return response.json();
}).then(result => {
    console.log(result);
})
Comments