javascript flask 网址混淆-获取404或500依赖于网址

w8biq8rn  于 2022-12-25  发布在  Java
关注(0)|答案(3)|浏览(158)

I am new to web programming. Writing a simple ajax call to run a python function via 2 files javascriptPython3.html & javascriptPython3.py
客户端是macbook和firefox 80.0.1网络服务器是rbp端口80的raspberry pi Apache/2.4.25(Raspbian)服务器
在我运行的服务器上:

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。
If I change the ajax url to "javascriptPython3.py" I get 500 Internal Server error.

#!/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 Python\n\n")

      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/时,正如我所希望的那样,它被附加了

zbq4xfa0

zbq4xfa01#

但是当我在客户机上浏览javascriptPython3.html时
正确。在您的代码中,javascriptPython3.html没有路由

非常高级/简单的解释

  1. Flask根据你在python代码中定义的路径返回(提供)内容。它可以以纯文本的形式返回内容,也可以通过模板(html页面)返回内容,例如render_template(<html_page>)
    1.在您的代码中,您只定义了一个路由-@app.route('/',它对应于http://192.168.1.6:5000/。请这样想-您在中定义为路由的任何内容都位于您的服务器地址http://192.168.1.6:5000之后
    1.如果您希望导航到javascriptPython3.html,即希望能够键入url http://192.168.1.6:5000/javascriptPython3.html,则必须在代码中为其定义一个路由,即需要类似app.route('/javascriptPython3.html')的内容
    1.由于您似乎希望在打开服务器时(即转到根目录/时)显示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 Python\n\n")

      return render_template('javascriptPython3.html')
huwehgph

huwehgph2#

我终于把它修好了我再次尝试通过 AJAX 调用简单地路由(运行)一个python函数。
我只需要使用包含IP和端口的完整URL,指向html文件中的apache服务器

AJAX URL:   url: "http://192.168.1.9:5000/foo",
python route:     @app.route('/foo', methods=['GET','POST'])
r1wp621o

r1wp621o3#

下面是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 Python\n\n")

      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>

相关问题