Ajax get()返回未定义的响应
我在aurelia项目中使用axios,并遵循他们的文档,我设置了一个基本的get请求,如下所示Ajax get()返回未定义的响应,ajax,rest,xmlhttprequest,axios,aurelia,Ajax,Rest,Xmlhttprequest,Axios,Aurelia,我在aurelia项目中使用axios,并遵循他们的文档,我设置了一个基本的get请求,如下所示 import Axios from '../../node_modules/axios/index'; export class testService { constructor() { this.axios = new Axios({ withCredentials: false, headers:{ "Accept": "applicati
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 () {
});
}
}
我有一个api服务器运行在“http://localhost:3000/items
“在Chrome和Postman中点击该url,我可以得到有效的JSON响应。但是,运行上述代码(即从调用test()
方法),记录的响应是undefined
我已经研究过类似问题的其他答案,但迄今为止没有一个对我有效。我做错了什么?新Axios({…配置…})签名中似乎有错误。根据,您需要
.create
方法来创建axios实例:
this.axios = Axios.create({
withCredentials: false,
headers:{
"Accept": "application/json"
},
baseURL: 'http://localhost:3000/'
});
谢谢,成功了。我确实看了,但似乎他们只是进一步解释了
create
部分。顺便说一句,你可以从“Axios”导入Axios而不是从“../../node_modules/Axios/index”导入Axios代码>是的,这只是VS代码的自动填充做它的事情。谢谢