gpt4 book ai didi

javascript - 如何在 javascript/Ajax 中添加重试

转载 作者:行者123 更新时间:2023-12-03 00:18:31 25 4
gpt4 key购买 nike

我是 javascript 和 Ajax 新手,如果 ajax 响应不是 200,则需要重试 3 次。

Ajax 函数 -

function fireAndForget(strURL) {
log("will try to invoke... [ " + strURL + " ]");
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
} // IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
self.xmlHttpReq.open('GET', strURL, true);
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
if(self.xmlHttpReq.status == 200) {
log("received JSON response : [" + self.xmlHttpReq.responseText + "]");
var resObj = parseJSON(self.xmlHttpReq.responseText);
if("handled" in resObj) {
if(resObj.handled) {
if("success" in resObj) {
if(resObj.success) {
// DO NOTHING
} else {
if(resObj.message) {
alert(resObj.message);
}
}
}
}
} else {
log("auth update notification was not handled. response : [" + self.xmlHttpReq.responseText + "]");
}
} else {
// unable to contact the auth update listener
alert("<%=pNotifyFailedMsg%>");
log("unable to contact listener URL @ [" + strURL + "]");
}
}
};
// fire a get request with the SSO information
self.xmlHttpReq.send(null);
//alert("sent url : [" + strURL +"]");
}

需要添加重试

if(self.xmlHttpReq.status == 200) {
log("received JSON response : [" + self.xmlHttpReq.responseText + "]");
var resObj = parseJSON(self.xmlHttpReq.responseText);
if("handled" in resObj) {
if(resObj.handled) {
if("success" in resObj) {
if(resObj.success) {
// DO NOTHING
} else {
if(resObj.message) {
alert(resObj.message);
}
}
}
}
} else {
log("auth update notification was not handled. response : [" + self.xmlHttpReq.responseText + "]");
}
} else {
// unable to contact the auth update listener
alert("<%=pNotifyFailedMsg%>");
log("unable to contact listener URL @ [" + strURL + "]");
}

我在上面代码的其他部分尝试了循环和其他一些解决方案,但没有成功,请帮忙。在这种情况下重试的好方法应该是什么

仅在 3 次重试后才必须显示警报(其他部分)

最佳答案

我假设makeRequest是描述的函数here如果你需要经典的 JS,你可以将 ES6 翻译为 const x = (a,b=0)=>(c)=>22function x(a,b){if( b===未定义){b=0}返回函数(c){...

您的重试构建器函数可能如下所示:

const createRetry = (
maxRetries, //how many times to try
passingResults = (id) => id, //function to "pass the result"
tries = 0, //how many times tried
) => (method, url) =>
makeRequest(method, url)
.then(passingResults)
.catch(
(error) =>
tries < maxRetries
? createRetry(//call itself again when it failed
maxRetries,
tries + 1,//add one to tries as we've just tried
passingResults,
)(method, url)
: Promise.reject(error),//tried max tries times, just reject
);

const retryTwiceAndNameIsInResponse = createRetry(
2, //retry twice
//function to check the xhr json result, if result has a name
// property then it passes and just returns the result,
// if not then it will reject the promise with an error
// a function can be optionally passed, if it's not passed
// then it will default to an id function (just returns what it gets)
(result) =>
result.name
? result
: Promise.reject(new Error('no name in result')),
//no need to pass tries in as that defaults to 0 and indicates how
// many times the method has tried to get the right result
);

//to use the function:
retryTwiceAndNameIsInResponse(method,url)
.then(response=>console.log('passed, response is:',response))
.catch(error=>console.log('we failed:',error))

关于javascript - 如何在 javascript/Ajax 中添加重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54436207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com