无法通过从 Ajax 中的 Flask 视图获取结果来生成警报消息
Unable to produce alert message via obtaining result from Flask view in Ajax
我的 HTML 代码片段
<button onclick="myfunc()">Try it</button>
<script type="text/javascript" src="./lib/jquery-3.3.1.min.js"></script>
<script>
function myfunc(){
setInterval("wowwah()",3000); // call every 3 seconds
};
function wowwah(){
$.ajax({
url:"/myStatus", //the Flask view containing python script
type:"post",
dataType:"json",
success:function(result){ alert(result);
}
});
}
</script>
我的 Flask 应用视图
@application.route('/myStatus', methods=['POST','GET'])
def check_status():
global have_plot
if not have_plot:
return jsonify('Not Ready')
else: return jsonify('Ready')
但是我的网络浏览器中没有弹出警告消息。我希望每隔 3 秒使用 Flask 应用程序
弹出警报消息并显示 'Ready' 或 'Not Ready'
我做错了什么?
可能您遇到了 HTTP 错误,但您没有看到是因为您的 Ajax 不处理 HTTP 错误。
尝试将您的 Ajax 更改为:
$.ajax({
url: "/myStatus", //the Flask view containing python script
type: "post",
dataType: "json",
success: function(result){ alert(result); },
error: function(result) { console.error(result); }
});
然后您将能够在开发者控制台中看到一个可能的错误。
我的 HTML 代码片段
<button onclick="myfunc()">Try it</button>
<script type="text/javascript" src="./lib/jquery-3.3.1.min.js"></script>
<script>
function myfunc(){
setInterval("wowwah()",3000); // call every 3 seconds
};
function wowwah(){
$.ajax({
url:"/myStatus", //the Flask view containing python script
type:"post",
dataType:"json",
success:function(result){ alert(result);
}
});
}
</script>
我的 Flask 应用视图
@application.route('/myStatus', methods=['POST','GET'])
def check_status():
global have_plot
if not have_plot:
return jsonify('Not Ready')
else: return jsonify('Ready')
但是我的网络浏览器中没有弹出警告消息。我希望每隔 3 秒使用 Flask 应用程序
弹出警报消息并显示 'Ready' 或 'Not Ready'我做错了什么?
可能您遇到了 HTTP 错误,但您没有看到是因为您的 Ajax 不处理 HTTP 错误。
尝试将您的 Ajax 更改为:
$.ajax({
url: "/myStatus", //the Flask view containing python script
type: "post",
dataType: "json",
success: function(result){ alert(result); },
error: function(result) { console.error(result); }
});
然后您将能够在开发者控制台中看到一个可能的错误。