当我的axios
调用失败时,catch()
中的代码正确显示了已定义的err.response.data.message中包含的错误消息。
但是当axios
调用成功时,我在控制台中收到此错误:
未捕获(承诺)TypeError:无法读取未定义的属性“ data”
该如何解决?
这是我的axios
通话代码:
mounted () {
this.$axios.$get(`http://example.com/wp-json/project/v1/post/${this.$route.params.id}`)
.then((res) => {
this.title = res.post_title
this.content = res.post_content
})
.catch((err) => {
this.$toast.error(err.response.data.message)
})
答案 0 :(得分:0)
尝试用res.data.post_title
代替res.post_title
...
.then((res) => {
this.title = res.data.post_title
this.content = res.data.post_content
})
...
Axios返回一个包含以下信息的对象
{
data: {}, // the response that was provided by the server
status: 200, // the HTTP status code from the server response
statusText: 'OK',// the HTTP status message from the server response
headers: {}, // the headers that the server responded with
config: {}, // the config that was provided to `axios` for the request
request: {} // the request that generated this response
}
如果您窃取了相同的错误,则可能是服务器端格式错误的数据,请尝试console.log响应并查看是否有问题。
...
.then((response) => {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});
...