mercredi 17 février 2016

Google ReCaptcha doesn't return errors

I'm trying to send the recaptcha key via AJAX, everything appears to be ok, but for some reason the php recaptcha sdk doesn't return errors, but the response is unsuccessful.

Here is my PHP code:

$recaptcha = new \ReCaptcha\ReCaptcha($user->secret);
        $resp = $recaptcha->verify($data['g-recaptcha-response'], request()->ip());

    if ($resp->isSuccess()) {
        //Success
    }else{
        return response()->json([
            'message' => 'Recaptcha error',
            'errors' => $resp->getErrorCodes(),
        ], 401);
    }

Ajax request:

$.ajax({
    type: 'POST',
    url: 'http://localhost/endpoint',
    datatype: 'json',
    cache: false,
    data: {
        email: document.getElementById('register_email').value,
        password: document.getElementById('register_password').value,
        password_confirmation:document.getElementById('register_re_password').value,
        first_name:document.getElementById('register_first').value,
        last_name:document.getElementById('register_last').value,
        'g-recaptcha-response': grecaptcha.getResponse(widgetId2)
    },
    success: function(result) {
        window.alert('Success');
        grecaptcha.reset(widgetId2);
    },
    error: function(result) {
        window.alert(result.responseJSON);
    }
});

And the response:

The secret is working because if I put a invalid g-recaptcha-response it throws the right error, the occurs same if I change the secret.

{message: "Recaptcha error", errors: []}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire