如何使此方法等待 axios 回應,然后應用該值進行重定向?http 請求的回應是 JSON。
async process(p) {
console.log('About to Call API');
console.log('Calling HTTP Service');
try {
const t = await axios.get("http://sampleendpoint-inside-intranet.net/apiservice?param=" p)
//var redirectURL = t.redirectURL;
//window.location.href = redirectURL;
return '';
}catch(error) {
console.log(error);
return 'error';
}
uj5u.com熱心網友回復:
回應資料在 中t.data,您可以嘗試記錄回應以查看其組成,然后您可以使用 重定向window.location.href = 'some_url'。
async process(p) {
console.log('About to Call API');
console.log('Calling HTTP Service');
try {
const t = await axios.get("http://sampleendpoint-inside-intranet.net/apiservice?param=" p)
console.log(t.data); // You can log the data to check what data you get returned here
window.location.href = t.data.redirectUrl;
} catch(error) {
console.log(error);
return 'error';
}
}
uj5u.com熱心網友回復:
您可以在 axios.get 函式之后使用 .then。這將在您收到回應后立即執行。
const t = await axios.get("http://sampleendpoint-inside-intranet.net/apiservice?param=" p)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
console.log(error);
return 'error';
})
在上面的示例中,我記錄了回應。
您也可以使用 .catch,這樣您就不再需要 try catch
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477784.html
