VUE -axios GET does not execute



My vue porject使用axios发送HTTP请求。

在安卓系统上运行没有问题。

在IOS上运行时,偶尔不会发送HTTP请求。

但并不是每个IOS手机都会出现这种情况,这种问题只能通过卸载和重新安装APP来解决。

getInfo() {
let params = { device_id: this.info_id.device_id };
axios.get(device_detail, params, this.headers).then(res => {
if (!res.code) {
this.product_id = res.data.device_type;
this.home_id = res.data.home_id;
localStorage.setItem("product_id", this.product_id);
localStorage.setItem("home_id", this.home_id);
localStorage.setItem("product_num_id", res.data.product_id);
localStorage.setItem("classify_id", res.data.classify_id);
this.getFuncList();
}
});
},

get(url, param, headers) {
return new Promise((resolve, reject) => {
axios({
method: "get",
url,
params: param,
headers: headers
}).then(res => {
resolve(res);
});
});
},
this.headers = {
token: this.token,
appid: this.appid,
lang:  this.$i18n.locale,
};

getInfo((在created((中运行

我使用localStorage.setItem((来保存数据;令牌"appid";。

我发现axios没有执行,我不知道为什么,但如果你卸载并重新安装应用程序,问题就解决了。

为什么????

原谅我英语不好。

我上次使用vue.js已经有一段时间了,但据我所知,您需要将getInfo((函数定义为async,因为它调用一个返回promise的函数。除此之外,通过使用wait,等待get函数的结果可以解决您的问题。我可能完全错了:(

尝试使用async/await——您的代码将更清晰,并与Vue的aysnchronous方法保持一致。还要将您的await封装在try/catch块中。这样,当它失败时,您将能够准确地看到错误是什么,从而了解它失败的原因。

async getInfo() {
try {
let params = { device_id: this.info_id.device_id };
const res = await axios.get(device_detail, params, this.headers);
if (!res.code) {
this.product_id = res.data.device_type;
this.home_id = res.data.home_id;
localStorage.setItem("product_id", this.product_id);
localStorage.setItem("home_id", this.home_id);
localStorage.setItem("product_num_id", res.data.product_id);
localStorage.setItem("classify_id", res.data.classify_id);
this.getFuncList();
};
return true;
} catch(err) {
console.error(err);
}
},

async get(url, param, headers) {
try {
const res = await axios({
method: "get",
url,
params: param,
headers: headers
});
return res;
} catch(err) {
console.error(err);
}
}

最新更新