Trying to use jQuery to read a file in Python Flask web application

Viewed 73

I have a flask and Python application where I have a Templates directory to hots an index.html that I render using

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

that works fine , but the web page index.html uses jquery to read a json file and for some reason it cannot read it in the FLASK webserver environment the json file is in a folder called ./curldata and the rendered html file is in a folder ./Templates at the same level

the error I get in developer tools ie:

jquery-3.5.0.js:10099 GET http://127.0.0.1:5000/curldata/views.json 404 (NOT FOUND)
send    @   jquery-3.5.0.js:10099
ajax    @   jquery-3.5.0.js:9682
jQuery.<computed>   @   jquery-3.5.0.js:9836
getJSON @   jquery-3.5.0.js:9817
(anonymous) @   (index):6
(anonymous) @   (index):18

see index.html below:

<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.0.js"></script>
<script>
(function() {  
$.getJSON( "../curldata/views.json", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    items.push( "<li><a href='" + val + "'>" + val + "</a></li>" );
  });
 
  $( "<ul/>", {
    "class": "my-new-list",
    html: items.join( "" )
  }).appendTo( "body" );
});
})();
</script>
</head>
<body>
BODY:</br>
</body>
</html>
1 Answers

I am an idiot , first off I am embedding the front end in the backend then I was'nt even using the backend

$.getJSON( "/views", function( data ) {

changing this line is the answer since the flask server is providing the json

sorry about that

Related