node.js - ReactJS: JSON file is fetched from localhost instead of project directory -


i have following directory structure of project.

enter image description here

i have following code in index.js file loading website.json file.

index.js

componentdidmount() {     $.ajax({        url: "../website.json",        type: "get",        datatype: 'json',        contenttype: 'application/json',        success: function(data) {                   this.setstate({data: data});          console.log(data);        }.bind(this),        error: function(jqxhr) {          console.log(jqxhr);        }.bind(this)     })   } 

the problem using npm start command server react app local directory of application. serves app @ http://localhost:3000/ . problem application tries load website.json file http://localhost:3000/website.json gives 404 not found error.

so, question how website.json file can loaded project directory instead of localhost.

note: project folder not @ localhost @ virtual host.

update: asking why ajax call unable load data project folder (i using relative addressing) , instead including path localhost. want load data using ajax call possible or not.

your webserver (i guess express) serving files 'public' folder.

for security reasons, web servers not allow access files outside of directory root (/public in case), not able file ajax (or browser).

if want to, either:

  • copy/move file public folder
  • create symlink file in public folder

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -