我是web编程新手。通过2个文件编写一个简单的ajax调用来运行一个python函数javascriptPython3.html,javascriptPython3.py
客户端是macbook &firefox 80.0.1Webserver是一个raspberry pi Apache/2.4.25 (Raspbian)服务器,rbp端口80
在服务器上我运行:
export FLASK_DEBUG=1
export FLASK_APP=javascriptPython3.py
flask run -h 192.168.1.6
* Serving Flask app "javascriptPython3"
* Forcing debug mode on
* Running on http://192.168.1.6:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger pin code: 318-477-733
192.168.1.3 - - [10/Dec/2022 14:44:55] "GET / HTTP/1.1" 200 -
192.168.1.3 - - [10/Dec/2022 14:44:57] "GET / HTTP/1.1" 200 -
192.168.1.3 - - [10/Dec/2022 14:44:58] "GET / HTTP/1.1" 200 -
在客户端macbook浏览器:http://192.168.1.6:5000/每次重新加载网页时,我都会在flask运行中得到一个输出行。
一切顺利。
但是当我在客户端浏览到javascriptPython3.html,并单击按钮时,我在浏览器控制台中没有找到404。
如果我将ajax url更改为"javascriptPython3.py"我得到500内部服务器错误。
#!/usr/bin/env python3
#from flask import Flask, request, render_template
from flask import Flask
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def makeupper():
# javascriptdata=request
# javascriptdata=javascriptdata.upper()
outputfile = open("javascriptPython.out", "a")
outputfile.write("Hello from Pythonnn")
return "ABCCC"
javascriptPython3.html:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
function successFun(pythonResponse) {
window.alert(pythonResponse);
document.getElementById('output1').value = pythonResponse;
}
function runPython() {
window.alert("We just called runPython()");
$.ajax({
type: "POST",
url: "/javascriptPython3.py",
// url: "/",
// data: "abc",
success: successFun()
});
}
</script>
</head>
<body>
<p> run python from javascrpt </p>
<br>
<input type="button" onClick="runPython()" value="Run Python" />
<br>
<br>
<p> Output text </p>
<textarea id="output1" rows="4" cols="50"> </textarea>
</body>
</html>
我期待看到javascriptPython。Out每次都被附加我点击了按钮。
当我重新加载http://192.168.1.6:5000/时它被附加了,就像我想要的那样
但是当我在客户端浏览到javascriptPython3.html时
正确。在您的代码中,没有javascriptPython3.html
的路由一个非常高级/简单的解释
Flask根据你在python代码中定义的路径(路由)返回(提供)内容。它可以以纯文本形式返回内容,也可以通过模板(html页面)返回内容,即
render_template(<html_page>)
在你的代码中,你只定义了一个路由-
之后@app.route('/'
对应http://192.168.1.6:5000/。这样想——任何你定义为路由的东西都在你的服务器地址http://192.168.1.6:5000如果你想导航到
javascriptPython3.html
,即你想要能够输入url http://192.168.1.6:5000/javascriptPython3.html,那么你必须在你的代码中为它定义一个路由,即你需要像app.route('/javascriptPython3.html')
这样的东西因为它看起来像你想要显示
javascriptPython3.html
当你打开你的服务器(即当你去你的根即/
),你应该修改你现有的代码
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/', methods=['GET','POST'])
def makeupper():
outputfile = open("javascriptPython.out", "a")
outputfile.write("Hello from Pythonnn")
return render_template('javascriptPython3.html')
我终于把它修好了;我再次试图通过ajax调用简单地路由(运行)一个python函数。
我只需要使用一个完整的URL与IP和端口,指向apache服务器在我的html文件
AJAX URL: url: "http://192.168.1.9:5000/foo",
python route: @app.route('/foo', methods=['GET','POST'])
下面是可以工作的2个文件
export FLASK_APP=javascriptPython3.py
export FLASK_DEBUG=1
flask run -h 192.168.1.9
Browse to: http://192.168.1.9/javascriptPython3.html
#!/usr/bin/env python3
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/foo', methods=['GET','POST'])
def foo():
# javascriptdata=request
# javascriptdata=javascriptdata.upper()
outputfile = open("javascriptPython.out", "a")
outputfile.write("Hello from Pythonnn")
return "ABCCC"
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.
min.js"></script>
<script>
function successFun(pythonResponse) {
window.alert(pythonResponse);
document.getElementById('output1').value = pythonResponse;
}
function runPython() {
window.alert("We just called runPython()");
$.ajax({
type: "GET",
url: "http://192.168.1.9:5000/foo",
// data: "abc",
success: successFun()
})
}
</script>
</head>
<body>
<p> run python from javascrpt </p>
<br>
<input type="button" onClick="runPython()" value="Run Python" />
<br>
<br>
<p> Output text </p>
<textarea id="output1" rows="4" cols="50"> </textarea>
</body>
</html>