javascript - Google Recaptcha Ajax response strange checking order -


i making registration page service. need check either google's recaptcha verified or not. decided use jquery.ajax.

i have created function "checkcaptcha()" sets iscapt (if captcha valid or not) either true (fld_valid) or not (fld_empty):

function checkcaptcha() {                 alert("1");                 var captcha_response_text = grecaptcha.getresponse();                  var request = $.ajax({                     url: "ajax/registrationa.php",                     type: "post",                     data: { captcha: true, captcha_response: captcha_response_text }                 });                  request.done(function (response, textstatus, jqxhr) {                     alert("2");                     if(response) {                         iscapt = fld_valid;                     }                     else {                         iscapt = fld_empty;                     }                 });             } 

i have "registrationa.php" works fine. no problems there.

after function, checking submit button handling "onclick" event:

apply_button.onclick = function () {                 checkcaptcha();                 alert("3");                 //alert("Капча " + iscapt + " Логин " + islogin + " Почта " + ismail + " Пароль " + ispass + " Соответствие " + ispas2);                 return false;             }; 

you can see 3 "alert(...)" operators. problem when press submit button (apply_button) 3 alerts: 1, 3, 2. how can fix problem. need wait until "requiest.done" executes , go "alert("3")". essential because function checks fields before checking captcha state leads error because in case iscapt equals false.

please me problem. maybe there better way check if captcha verified or not (maybe there function "grecaptcha.isverified").

you need check deferred.promise() method work asynchronous data. check jq documentation that. in few words need. allows wait until 2nd asynchronous request finished , starts 3d function.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -