Last active
November 18, 2017 12:36
-
-
Save toshimasa-nanaki/cb780201c016a4dc7436f7807159d9e7 to your computer and use it in GitHub Desktop.
Hello World API Python Request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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