Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JSONify返回奇怪的值_Javascript_Python_Jquery_Ajax_Python 3.x - Fatal编程技术网

Javascript JSONify返回奇怪的值

Javascript JSONify返回奇怪的值,javascript,python,jquery,ajax,python-3.x,Javascript,Python,Jquery,Ajax,Python 3.x,我试图通过AJAX将Python函数的结果返回给javascript。目前,当我期待“真”或“假”时,我得到了 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 Ch

我试图通过AJAX将Python函数的结果返回给javascript。目前,当我期待“真”或“假”时,我得到了

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)
您是否尝试过:

return jsonify({result: True})

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

试试这个:

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

我有几个问题

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

      $.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);
  });
$.getJSON(“/chk_-chn”,{name:channel_-name})
.done(函数(json){
log(json.result)
//检查名称唯一性
if(json.result==false){
$(“#chn_name”)。在('频道名称已存在')之后;
}
否则{
//检查通道长度
如果(通道名称长度>20){
$(“#chn_名称”)。在('频道名称超过最大长度')之后;
返回错误
}
否则{
//创建频道
emit(“create_channel”,{“channel_name”:channel_name})
//关闭模式
返回true;
}
}
})
.fail(函数(jqxhr、textStatus、error){
var err=textStatus+“,”+错误;
log(“请求失败:+err”);
});
我的第二个甚至更愚蠢的问题是,Ajax查询是由模式中存在的按钮调用的。当点击按钮时,模式关闭,javascript在新页面上重新生成,从而完全放弃我的查询

我通过在表单上返回false来修复此问题

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

我得到了完全相同的回答