【发布时间】:2015-02-01 12:20:01
【问题描述】:
一切都在标题中,我有一个按钮,在他附近有一个在开始时写“默认”的文本。当有人点击按钮时,我想发送“您收到了吗?”使用 ajax JavaScript 请求结束 server.py 回答“是”。
我的 server.py:
#! /usr/bin/python
# -*- coding:utf-8 -*-
from flask import Flask, json, request, render_template
app = Flask(__name__)
@app.route("/", methods=['GET', 'POST'])
def get_data():
if request.method == 'POST':
data = json.loads(request.form.get('data'))
ss = data['messageClient']
return str(ss)
return render_template('test.html')
if __name__ == '__main__':
app.run(debug=True)
我的 test.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Test</title>
</head>
<body>
<button id="UID_boutonTest" onclick="functionTest()" style='left:50px;height:50px;width:200px;'>boutonTest</button>
<span id="UID_afficheTest" style='text-align: center;'>defaut</span>
<script type="text/javascript">
function functionTest()
{
alert("click bouton");
$.ajax(
{
type : 'POST',
url : "/",
dataType:'json',
data : JSON.stringify( { "message" : "didyoureceive?" } ),
success : function(data)
{
if (data !=null)
{
messageServer=data;
$('#UID_afficheTest').val(messageServer);
document.getElementById('UID_afficheTest').innerHTML = messageServer;
alert("messageServer = "+messageServer);
}
}/*success : function() {}*/
});/*$.ajax*/
}
</script>
</body>
</html>
【问题讨论】:
-
坦克为您提供帮助,您无法想象我遇到了多大的麻烦。所以没有错误,如果我点击按钮,就会出现第一个警报“点击按钮”,但 server.py 中没有任何内容,所以函数成功没有任何返回
-
将 contentType: "application/json" 也放入您的 ajax 调用中。你能看到你的浏览器网络标签,请求是什么?
-
再次感谢您的帮助(对不起我的英语,我是法语)我在我的 ajax 请求中添加了 contentType: "application/json"。但你是对的,如果我使用 firebug,我会去网络,当我点击按钮时没有请求。
-
点击按钮时是否收到“点击按钮”提示?
-
yes 这部分有效,所以可能是我的 ajax 请求错误?