从ajax收到的数据获取请求

我有flask应用程序,我正尝试从客户端向后端发出请求,反之亦然,以验证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

以python方法打印的数据正确{'success': True,'challenge_ts': '2019-11-07T11:07:22Z','hostname': 'localhost'}。但是随后以js打印回的数据显示为:[object Object]。我应该如何正确读取从python verify_recaptcha方法返回的数据?

zjfshy 回答:从ajax收到的数据获取请求

您的代码正确。问题是在对象上调用.toString()将返回该值。如果您想查看包含对象的日志,请尝试:

window.console.log(data)

或:

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

.toString应用于对象将返回[object Object]

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

尝试直接使用对象属性,如下所示:

console.log(data.success);

还有一些建议:永远不要在公共场合显示您的API密钥

本文链接:https://www.f2er.com/3145350.html

大家都在问