Search code examples
javascriptpythonajaxflaskrecaptcha

Data received from ajax get request


I've got flask app and I'm trying to make a request from the client to backend and the other way round to validate ReCaptcha.

JS:

var onloadCallback = function() {

    var captchaCallback = function(param) {
        return $.get( "gettoken/" + param, function( data ) {
          window.console.log(data.toString())
          if (!data.success) {
              window.alert("something went wrong" + data.error);
          }
          else {
              $(".submitBtn").prop("disabled", false);
          }
        });
  };

  grecaptcha.render('html_element', {
  'sitekey' : 'secret_key',
    'callback' : captchaCallback
    });
};

PYTHON:

@app.route('/gettoken/<g_recaptcha_response>')
def verify_recaptcha(g_recaptcha_response):
    with urllib.request.urlopen('https://www.google.com/recaptcha/api/siteverify?secret=secret_key&response=' + g_recaptcha_response) as url:
        data = json.loads(url.read().decode())
        print(data)
        return data

Data printed in python method is correct {'success': True, 'challenge_ts': '2019-11-07T11:07:22Z', 'hostname': 'localhost'}. But then data printed back in js shows: [object Object]. How should I correctly read the data return from python verify_recaptcha method?


Solution

  • .toString applied for an object will return [object Object]

    var myObj = {};
    console.log(myObj.toString());
    //returns [object Object]
    

    Try to use object attributes directly, like this:

    console.log(data.success);
    

    And just as advice: never show your API keys on public