0

I have following directory structure of my project.

enter image description here

I have following code in my index.js file for 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 is that I am using npm start command to server my react app from local directory of my application. This serves my app at http://localhost:3000/ . Now the problem is that the application tries to load the website.json file from http://localhost:3000/website.json which gives a 404 not found error.

So, my question is how my website.json file can be loaded from my project directory instead of localhost.

Note: my project folder is not at localhost but at virtual host.

Update: I am specifically asking why the ajax call is unable to load data from my project folder (I am using relative addressing) and instead including path from localhost. I want to load data using ajax call is it possible or not.

1
  • Show what you running as 'start'. You may find it in package.json file. Commented Aug 14, 2017 at 15:14

3 Answers 3

2

Your webserver (I would guess Express) will be serving files from the 'public' folder.

For security reasons, web servers do not allow access to files outside of the directory root (/public in your case), so you will not be able to get the file by ajax (or from the browser).

If you really want to, you could either:

  • Copy/Move the file into the public folder
  • Create a symlink to the file in the public folder
Sign up to request clarification or add additional context in comments.

2 Comments

@Nosyara has mentioned this below (not as clearly as above, however).
Thank @Alcinator you made me understand it.
2

You may include it inside source as

var website = require('../website.json');

So it will be embedded during compile time.

Another way to do it - move website.json to public folder, than you may access it as ajax('/website.json'...

7 Comments

Is it not possible by using ajax that is my question?
Yes, if you add API on back end. And than use relative URL to get the file by ajax.
can you please explain it?
You have to manage server side. Or move your file to public folder and ajax it from root.
Currently I am doing it on my local machine. For sure I will move it to server. So I want to simulate here at localhost. Later on i will be providing a different url at server side.
|
0

I assume you are using ES6/ES2015 since you are using react. So instead of doing it in componentDidMount you can just add this at the top:

import websiteData from '../website.json';

And then you can just use websiteData as a variable in your component.

2 Comments

Yes it is possible by this method but my question is how to do it using a ajax call. I am updating the question to make it more clear.
Why do you need to do it via ajax? Is the json file changing? if not then doing it this way is perfectly suitable. If it is changing then componentDidMount would only load it once anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.