Python Flask routes not found when using AJAX to POST [duplicate]


Python Flask routes not found when using AJAX to POST [duplicate]



This question already has an answer here:



I am trying to route my Flask routes. When I just use / to route, it seems to work but it seems to be returning apache index page in my console (the whole HTML page, where my localhost runs) and I don't even see "hello world". When I try to make an ajax request to /index console shows 404 not found. Since I have an extra directory in my to wrap my application, I originally thought I had to add the prefix to my ajax request, so then I tried /my_dir/index but still not found. Any ideas?


/


/index


/my_dir/index



here is my directory listing


/localhost
/my_dir
/js
/templates
app.py



Here is my code:



html:


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>

<h1>USB Project</h1>


Status:
<textarea id="statusOutput" readonly></textarea>
</div>

Generate Report

http://../js/jquery-3.2.1.min.js
http://../js/jquery-ui.min.js
http://../js/usb.js
</body>
</html>



js:


$(function(){
$('button').click(function(){
var user = "user";
//var pass = $('#inputPassword').val();
$.ajax({
url: '/index',
data: user,
type: 'POST',
success: function(response){
console.log(response);
},
error: function(error){
console.log(error);
}
});
});
});



app.py:


from flask import Flask, request, send_file, render_template, json
from ftplib import FTP

app = Flask(__name__)

@app.route('/')
def hello_world():
return 'hello world!'

@app.route('/index')
def index():
return render_template('index.html')

if __name__ == "__main__":
app.run()



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Did you call app.run() in your app.py? Run the Flask app like: app.run(host='0.0.0.0', debug=True, threaded=True, port=<some-port-number>)
– Roger Huang
Jul 3 at 2:59






@RogerHuang Yes, sorry, i do have that in my code. I just updated the code.
– kkmoslehpour
Jul 3 at 3:01





Use $ curl localhost:<some-port-number> to see if your flask app receives the http request, there should be some useful logs in the console.
– Roger Huang
Jul 3 at 3:04





@RogerHuang OH do i have to manually run the flask before i run the ajax request?
– kkmoslehpour
Jul 3 at 3:06






Yes. You have to run the server locally (and maybe deploy to other place later).Use commands like: $ python app.py and you should be able to see: * Running on 127.0.0.1:5000 for more info: flask.pocoo.org/docs/1.0/quickstart
– Roger Huang
Jul 3 at 3:09




1 Answer
1



Try to add POST with acceptable method


@app.route('/index', methods=['GET', 'POST'])
def index():
return render_template('index.html')





@app.route('/index') and do curl -X POST http://127.0.0.15000/index -d'test' I've gotta 405 method not allowed response
– ohrlando
Jul 3 at 3:35


@app.route('/index')


curl -X POST http://127.0.0.15000/index -d'test'





Typo? $ curl -X POST 127.0.0.1:5000/index -d'test' ?
– Roger Huang
Jul 3 at 3:39





127.0.0.1:5000 :)
– ohrlando
Jul 3 at 3:45


127.0.0.1:5000





It seems to be working curl -X GET http://127.0.0.1:5000/index -d'test' but my AJAX call seems to not be hitting that port. This is running on a webserver do i need some special config for the AJAX call to make the call the port 5000?
– kkmoslehpour
Jul 3 at 17:46


curl -X GET http://127.0.0.1:5000/index -d'test'

Popular posts from this blog

PHP contact form sending but not receiving emails

PHP parse/syntax errors; and how to solve them?

iOS Top Alignment constraint based on screen (superview) height