简体   繁体   中英

Internal Server Error when sending json data from javascript to python

I have a page written in PHP that has a portion of javascript in it, and I am trying to get a function to send some JSON data to a python file. I am really new to Flask and though I've read the documentation, nothing has worked for me. This is my Javascript function:

<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script>
$(document).ready(function () {
.
.
.
function encrypt() {
  $.ajax({
    type: "POST",
    url: "/crypt",
    contentType: "application/json",
    data: JSON.stringify(generateParamsU()),
    dataType: "json",
    success: function(response) {
        console.log(response);
    },
    error: function(err) {
        console.log(err);
    }
  });
} 

My corresponding python code is:

from flask import Flask

app = Flask(__name__)

from app import routes

@app.route('/crypt', methods=['POST', 'GET'])
def crypt():
    data = request.get_json()
    return jsonify(data)

I've set up something really simple just to receive/send data. This is on a server (it's on cpanel ), and both files are on the same directory.

When I have it setup the way it is NOW, I get this error:

http://www.myweb.org/crypt 404 (Not Found)

When I change the AJAX post url to be url: "crypt.py" (the name of the file), I get this error:

POST http://www.myweb.org/custom/crypt.py 500 (Internal Server Error)

I really have no idea what the cause of this is. I am assuming that I am routing things wrong, but this is the way I've seen it done online everywhere. I am not sure...

One thing I think is the problem is that I am not in any way running the python file. If I am not running the python file, the Flask server won't spin up, right? I am not sure. Any help would be appreciated.

Thank you

Some things to clarify first.

I don't know cpanel in detail, but i guess you are missing some details. Apache or nginx, which might be running at your server on port 80 don't have a default handler for python, especially not for integrating a flask app, that's the reason you are getting a 500 when calling crypt.py

You should first start running Flask as standalone, flask is by default listening on port 5000. In order to run your flask app as standalone, add the following to your code.

app.run(host='0.0.0.0', debug=True)

Run method accepts further arguments: http://flask.pocoo.org/docs/1.0/api/#flask.Flask.run

Now you can start your flask standalone server with

python crypt.py

and the flask server will start listening on port 5000.

You should be able to POST the data to http://www.myweb.org:5000/crypt now.

If you wan't to integrate your flask app into apache or nginx, you should have a look configuring a reverse proxy or uwsgi, but both are more advanced. First you should understand the basics and get your app running as standalone in flask debug mode.

As your flask app is now listening on port 5000 and not 80, you also need to change your ajax request to post to: http://www.myweb.org:5000/crypt , i am not famliar with javascript/ajax, but i guess

url: "http://www.myweb.org:5000/crypt",
should be sufficient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM