JSONify返回奇怪的值

时间:2018-10-10 15:01:01

标签: javascript python jquery ajax python-3.x

我想做的是通过AJAX将Python中函数的结果返回给我的javascript。目前,我在等待“ True”或“ False”的同时得到This Response

jquery:

  var test = $.getJSON("/chk_chn", {
    name: channel_name
  });

  alert(test.toSource())

python:

@app.route("/chk_chn")
def chk_chn_unique():
"""Checks a name against all existing Channels in a Channel List. Returns True if name is unique, False otherwise"""
name = request.args.get("name")
for channel in Channels:
    if channel.get_name() == name:
        return jsonify(result=False)
return jsonify(result=True)

3 个答案:

答案 0 :(得分:1)

您缺少回调函数,只是打印出请求对象。

尝试一下:

$.getJSON('/chk_chn', { name: channel_name })
    .done(function (data) {
        console.log(data);
    });

答案 1 :(得分:0)

您尝试过吗:

return jsonify({result: True})

答案 2 :(得分:0)

我有几个问题。

首先,我的Ajax查询没有任何回调函数。感谢Rawri指出这一点。现在的代码如下。

      $.getJSON("/chk_chn", { name: channel_name} )
  .done(function( json ) {
    console.log(json.result)
    // Check Name Uniqueness
    if (json.result === false) {
      $("#chn_name").after('<span class="error">Channel name already exists</span>');
    }
    else {
      // Check Channel Length
      if (channel_name.length > 20) {
        $("#chn_name").after('<span class="error">Channel Name exceeds maximum length</span>');
        return false
      }
      else {
        // Create Channel
        socket.emit("create_channel", {"channel_name": channel_name})
        // Close the modal
        return true;
      }
    }
  })
  .fail(function(jqxhr, textStatus, error) {
    var err = textStatus + ", " + error;
    console.log("Request Failed: " + err);
  });

我的第二个甚至更愚蠢的问题是,模态中存在的按钮正在调用Ajax查询。单击按钮后,模态已关闭,并且在新页面上重新生成了javascript,因此完全放弃了我的查询。

我通过在表单上返回false来解决此问题

<form role="form" id="submit_channel" onsubmit="return false">