简体   繁体   中英

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?

Your code is correct. The problem is calling .toString() on an object will return that. If you want to see a log with your object try with:

window.console.log(data)

or:

window.console.log(JSON.stringify(data, null, 2))

.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM