Skip to content

Instantly share code, notes, and snippets.

@toshimasa-nanaki
Last active November 18, 2017 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toshimasa-nanaki/cb780201c016a4dc7436f7807159d9e7 to your computer and use it in GitHub Desktop.
Save toshimasa-nanaki/cb780201c016a4dc7436f7807159d9e7 to your computer and use it in GitHub Desktop.
Hello World API Python Request
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', title='test', userName='kentei')
@app.route('/hello', methods=['GET','POST'])
def hello():
if request.method == 'POST':
name = request.form['name']
else:
name = "no name."
return name + " Hello world!"
if __name__ == "__main__":
app.run()
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$('#hello').on("click", function() {
$.ajax({
url: "http://127.0.0.1:5000/hello",
type:'POST',
dataType: 'text',
data : {"name": "Kentei"},
timeout:10000,
}).done(function(data) {
alert(data);
}).fail(function(XMLHttpRequest, textStatus, errorThrown) {
alert("error");
})
});
});
</script>
<h1>ようこそ、{{userName}}</h1>
<input type="button" value="Hello Button" id="hello"/>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment