xmlhttprequest python

Solutions on MaxInterview for xmlhttprequest python by the best coders in the world

showing results for - "xmlhttprequest python"
Maria
10 Sep 2016
1#xmlhttprequest on python:
2
3#index.html
4
5<!DOCTYPE html>
6<html>
7  <head>
8    <title>Practice AJAX</title>
9    <script type="text/javascript">
10      function do_ajax() {
11        var req = new XMLHttpRequest();
12        var result = document.getElementById('result');
13        req.onreadystatechange = function()
14        {
15          if(this.readyState == 4 && this.status == 200) {
16            result.innerHTML = this.responseText;
17          } else {
18            result.innerHTML = "処理中...";
19          }
20        }
21
22        req.open('POST', '/', true);
23        req.setRequestHeader('content-type', 'application/x-www-form-urlencoded;charset=UTF-8');
24        req.send("name=" + document.getElementById('name').value);
25      }
26    </script>
27  </head>
28  <body>
29    <form action="index" method="post">
30      <label>Name:<input type="text" id="name" value="" /></label>
31      <button type="button" id="btn-post" onclick="do_ajax();">Click</button>
32      <div id="result"></div>
33    </form>
34  </body>
35</html>
36
37#server.py
38
39from flask import Flask, request, render_template
40
41app = Flask(__name__)
42app.debug = True
43
44
45@app.route("/", methods=['GET', 'POST'])
46def index():
47    if request.method == "POST":
48        name = request.form["name"]
49        return name + " Hello"
50    return render_template("index.html")
51
52
53if __name__ == "__main__":
54    app.run()