所以我不知道是什么导致了这个问题,而且我花了至少30分钟搜索谷歌并尝试了不同的解决方案。我已经定义了一个async
函数,我试图在其中使用await
但是它给了我
错误:未捕获的SyntaxError:await仅在异步函数中有效
以下是代码:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function SolveRecaptchaV2(APIKey, googleKey, pageUrl, proxy, proxyType){
var requestUrl = "https://2captcha.com/in.php?key=" + APIKey + "&method=userrecaptcha&googlekey=" + googleKey + "&pageurl=" + pageUrl + "&proxy=" + proxy + "&proxytype=";
switch (proxyType) {
case 'HTTP':
requestUrl = requestUrl + "HTTP";
break;
case 'HTTPS':
requestUrl = requestUrl + "HTTPS";
break;
case 'SOCKS4':
requestUrl = requestUrl + "SOCKS4";
break;
case 'SOCKS5':
requestUrl = requestUrl + "SOCKS5";
break;
}
$.ajax({url: requestUrl, success: function(result){
if(result.length < 3){
return false;
}else{
if(result.substring(0, 3) == "OK|"){
var captchaID = result.substring(3);
for(var i=0; i<24; i++){
var ansUrl = "https://2captcha.com/res.php?key=" + APIKey + "&action=get&id=" + captchaID;
$.ajax({url: ansUrl, success: function(ansresult){
console.log(ansresult);
if(ansresult.length < 3){
return ansresult;
}else{
if(ansresult.substring(0, 3) == "OK|"){
return ansresult;
}else if (ansresult != "CAPCHA_NOT_READY"){
return ansresult;
}
}
}
});
await sleep(1000);
}
}else{
return ansresult;
}
}
},
fail: function(){
return "";
}
});
}
编辑::现在,当我将$ .ajax回调函数设为异步函数时,$ .ajax调用都不起作用,并且任何console.log&#39; si内部都不会显示在控制台中..我虽然没有任何错误
答案 0 :(得分:1)
所以这里的主要问题是,你在$.ajax
函数的成功函数中使用await(这是不可能的,不能使这个函数同步,但现在因为你的代码混合了回调/异步,你无法解决SolveRecaptchaV2
的原始承诺。你将异步与旧的回调混合在一起。)你的代码基本上是这样做的:
SolveRecaptchaV2 => Ajax Request => Ajax Request Done, Firing callback
V V
Returning Returning (But nothing cares about this return)
如果您正在使用异步功能,请尝试尽可能多地使用异步。 $.ajax
将返回一个promise,因此我们可以等待,并保持我们的函数完全异步:
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function SolveRecaptchaV2(APIKey, googleKey, pageUrl, proxy, proxyType){
var requestUrl = "https://2captcha.com/in.php?key=" + APIKey + "&method=userrecaptcha&googlekey=" + googleKey + "&pageurl=" + pageUrl + "&proxy=" + proxy + "&proxytype=";
switch (proxyType) {
case 'HTTP':
requestUrl = requestUrl + "HTTP";
break;
case 'HTTPS':
requestUrl = requestUrl + "HTTPS";
break;
case 'SOCKS4':
requestUrl = requestUrl + "SOCKS4";
break;
case 'SOCKS5':
requestUrl = requestUrl + "SOCKS5";
break;
}
try {
await result = await $.ajax({url: requestUrl});
if(result.length < 3) {
return false;
} else {
if(result.substring(0, 3) == "OK|") {
var captchaID = result.substring(3);
for(var i=0; i<24; i++){
var ansUrl = "https://2captcha.com/res.php?key=" + APIKey + "&action=get&id=" + captchaID;
var ansResult = await $.ajax({url: ansUrl});
console.log(ansResult);
if(ansResult.length < 3) {
return ansResult;
} else {
if(ansresult.substring(0, 3) == "OK|") {
return ansResult;
} else if (ansResult != "CAPCHA_NOT_READY") {
return ansResult;
}
}
await sleep(1000);
}
} else {
// ansResult is not defined here, not sure what you want to return here (May want to return false, or an empty string):
return ansResult;
}
}
} catch (err) {
//On ajax failure, return empy string. (May want to return false here, to fall inline with your "if (result.length < 3)" statement above.)
return "";
}
}
现在我们的链看起来像这样:
SolveRecaptchaV2 => Ajax Request
=> Ajax Request Done
=> Get Answer URL
=> Sleep
=> Get Answer URL
=> Sleep (...loop)
=> Resolve promise of SolveRecaptchaV2.