axios.get() returns 未定义的响应
axios.get() returns undefined response
我在 aurelia 项目中使用 axios 并遵循他们的文档,我设置了一个基本的获取请求,就像这样
import Axios from '../../node_modules/axios/index';
export class testService {
constructor() {
this.axios = new Axios({
withCredentials: false,
headers:{
"Accept": "application/json"
},
baseURL: 'http://localhost:3000/'
});
}
test() {
this.axios.get('/items')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
}
}
我在“http://localhost:3000/items
”有一个 api 服务器 运行 并在 Chrome 和 Postman 中点击 url,我可以获得有效 JSON 响应。但是,运行 上面的代码(即从中调用 test()
方法),记录的响应是 undefined
.
我查看了类似问题的其他答案,但 none 到目前为止对我有用。我在这里做错了什么?
new Axios({ ...config ... })
签名似乎有误。根据their doc,你需要.create
方法来创建axios实例:
this.axios = Axios.create({
withCredentials: false,
headers:{
"Accept": "application/json"
},
baseURL: 'http://localhost:3000/'
});
我在 aurelia 项目中使用 axios 并遵循他们的文档,我设置了一个基本的获取请求,就像这样
import Axios from '../../node_modules/axios/index';
export class testService {
constructor() {
this.axios = new Axios({
withCredentials: false,
headers:{
"Accept": "application/json"
},
baseURL: 'http://localhost:3000/'
});
}
test() {
this.axios.get('/items')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
})
.then(function () {
});
}
}
我在“http://localhost:3000/items
”有一个 api 服务器 运行 并在 Chrome 和 Postman 中点击 url,我可以获得有效 JSON 响应。但是,运行 上面的代码(即从中调用 test()
方法),记录的响应是 undefined
.
我查看了类似问题的其他答案,但 none 到目前为止对我有用。我在这里做错了什么?
new Axios({ ...config ... })
签名似乎有误。根据their doc,你需要.create
方法来创建axios实例:
this.axios = Axios.create({
withCredentials: false,
headers:{
"Accept": "application/json"
},
baseURL: 'http://localhost:3000/'
});