烧瓶javascript简单的ajax请求

时间:2015-02-01 12:20:01

标签: ajax flask request

一切都在标题中,我有一个按钮,并且在他附近有一个文本,它在开始时写为“默认”。当有人点击按钮我想发送“你收到了吗?”用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>

非常感谢

1 个答案:

答案 0 :(得分:0)

好的,现在我的ajax请求正常,server.py返回好的价值(如果我用firebug检查,我看到:问:“你有没有”并且回答:“我收到了:你收到了吗?所以它有效但是, 我的文字没有改变,我不明白为什么我的javascript警报没有出现(alert1和4工作不是其他人)

<!DOCTYPE html>

<html>
    <head>
      <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
      <script type="text/javascript">
          function functionTest()
          {
          alert("alert1:click bouton");
            $.ajax(
            {
              type : 'POST',
              url : "/lolo",
              contentType: "application/json;charset=UTF-8",
              dataType:'json',
              data    : JSON.stringify( { "messageClient" : "didyoureceive?" } ),
              success : function(data)
              {
                alert("alert2:messageServer = "+data);
                if (data !=null)
                {
                  alert("alert3:messageServer = "+data);
                  messageServer=data;
                  $('#UID_afficheTest').val(messageServer);
                  document.getElementById('UID_afficheTest').innerHTML = messageServer;

                }
              }/*success : function() {}*/
            });/*$.ajax*/
          alert("alert4:after ajax request");
          }
        </script>
      <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>

    </body>
</html>

和我的服务器:

#! /usr/bin/python                                                                        
# -*- coding:utf-8 -*-                                                                    

from flask import Flask, json, request, render_template
app = Flask(__name__)

@app.route("/lolo", methods=['GET', 'POST'])
def get_data():
    if request.method == 'POST':                                  
        data = json.loads(request.data)
        ss = data['messageClient']
        ss = "I received : " + ss
        return str(ss)
    return render_template('test.html')

if __name__ == '__main__':
    app.run(debug=True)