Donnie
Donnie

Reputation: 6351

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());
});

Upvotes: 0

Views: 1633

Answers (1)

Bergi
Bergi

Reputation: 664503

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);
})

Upvotes: 5

Related Questions