尋找幾個小時後,我結束了有這個和它完美的作品。希望這將有助於未來的其他人。
HTML and JS code -- loging.html
<html>
<head>
<title>Flask Intro - login page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="static/bootstrap.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="http://code.jquery.com/jquery 2.1.4.min.js"></script>
</head>
<body>
<input id="submitbutton" type="submit" value="Test Send Data">
<!----------------------------------->
<script type="text/javascript">
function runPyScript(input){
var jqXHR = $.ajax({
type: "POST",
url: "/login",
async: false,
data: { mydata: input }
});
return jqXHR.responseText;
}
$('#submitbutton').click(function(){
datatosend = 'this is my matrix';
result = runPyScript(datatosend);
console.log('Got back ' + result);
});
</script>
Python code -- app.py
from flask import Flask, render_template, redirect, url_for,request
from flask import make_response
app = Flask(__name__)
@app.route("/")
def home():
return "hi"
@app.route("/index")
@app.route('/login', methods=['GET', 'POST'])
def login():
message = None
if request.method == 'POST':
datafromjs = request.form['mydata']
result = "return this"
resp = make_response('{"response": '+result+'}')
resp.headers['Content-Type'] = "application/json"
return resp
return render_template('login.html', message='')
if __name__ == "__main__":
app.run(debug = True)
'〜/ reverse_pca.py'是相對路徑,它看起來像它意味着是一個絕對路徑。無論如何,你應該提及任何錯誤信息(因爲「不工作」可能意味着很多事情),你應該觀看Web服務器日誌。 – betterworld