我撰寫了這段代碼,它對我來說非常完美。但是當我嘗試將其轉換為 fetch 時,因為回傳了一個承諾,所以它不起作用。我不知道如何在獲取時將其用作異步錯誤。
'getProjectConstants': function(constArray)
{
var result='';
$.ajax({
type: "GET",
url: `${constProjectBaseUrl}/ajax/getProjectConstants/${constArray}`,
async: false,
dataType: "json",
success: function(data) {
result = data
}
});
return result
我試圖這樣做,但不作業
fetch(`${constProjectBaseUrl}/ajax/getProjectConstants/${constArray}`)
.then((response) => response.json())
.then((response) => {
// console.log(response)
return response
}) // end of .then((response) => {
.catch(error => {
console.log("[ajax.getProjectConstants] Error => " error);
}); // end of .catch(error => {
uj5u.com熱心網友回復:
你可以試試這個:
async function getProjectConstants(){
const json = await (await fetch(`${constProjectBaseUrl}/ajax/getProjectConstants/${constArray}`)).json();
return json;
}
ajax.getProjectConstants(['PROJECT_PASSWORD_MINIMUM', 'OWNER_NAME', 'DB_TBL_TIME_ZONE']).then((constant) => {
console.log(constant.OWNER_NAME)
}).catch(console.error);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/461025.html
標籤:javascript jQuery 阿贾克斯 拿来
