Share On Twitter Facebook Google+ LinkedIn Pinterest Tumblr Reddit
Question

Running the JSON file on the local server and accessing it with javascript

Tags: javascript json
Date:
Status:Resolved
Question Id:22

Hello.I wanted to access data.json file on my computer with js. Then I learned that json file must be on a server to access js. To run the server, I switched the data.json file to the directory where it is located and ran the server with the following command: python3 -m http.server. When I type http://127.0.0.1:8000/data.json in the address cubug, the server is working properly and it appears in json data. But when I try to access this file with js like this:

  var request = new XMLHttpRequest()
      request.open("GET","http://127.0.0.1:8000/data.json",true)
      request.onload = function () {
          var data = JSON.parse(this.response)
          console.log(data);
        }

For some reason I can not get any output. Where is the problem? I am running my server incorrectly, I am accessing incorrectly with js or am I following a wrong path in general? I will be glad if you can help.

 

Answer
Date:
Correct:No

Additional information:

The requests I have sent are not seen on the server. Or the request is not sent. I think the problem is on the javascript side.

Answer
Date:
Correct:No

Nobody can help without codes.

Answer
Date:
Correct:No

what's code I shared the js code above. I think it is not necessary if it is the data in the json file.

but let me add anyway:

{
  "name":"jack",
  "age":22
}
Answer
Date:
Correct:No

Is there an error in the page console (ctrl + shift + k)?

 

You can be CORS, you can put the page next to json and try to run it from there as the easiest workaround.

Answer
Date:
Correct:No

No.I am trying to print the data to the console and cannot get any output.

no, it does not work as I wish. For example, I am sending data to the json file, it was sent ok, but when I refresh the page, the data I added goes (naturally)

Answer
Date:
Correct:No

Where does the data you add, where does the data go? What does the added data look like?

Which page are we talking about by the way?

If the output of a command such as console.log ("test") does not appear on the console, it means there is a much bigger problem irrelevant to the topic.

 

Answer
Date:
Correct:No

no, there is no problem with the console.

I'm sorry. I meant to write to the file.

I want to write the json data contained in the javascript to the .json file.

Answer
Date:
Correct:No

Does something written to the file on the server side get lost when the client page refreshes? I did not understand.

Reminding that there is no file on the client side, there are URLs and requests, I would like to ask again what he tried to try.

For this, first of all, it is necessary to send the JSON data in the javascript to the server that has access to the file system. I recommend using an HTTP POST request for this.

Then we can determine what works and what does not by adding console.log where necessary.

By the way, I saw it right now, first the request is made (open), then it is stated what will happen when the request is made (onload).

Answer
Date:
Correct:No

I tried to access it via POST method as well, but nothing has changed

Answer
Date:
Correct:No

I need to handle the POST on the server side.

There was a CORS problem.

The last problem I wrote stops.

Answer
Date:
Correct:No

How will that be, teacher?

How can I solve it?

By the way, I saw it right now, first the request is made (open), then it is stated what will happen when the request is made (onload). Didn't you say? isn't it like you said anyway?

Answer
Date:
Correct:No

Since the code to run after the request is not defined at the point where the request is made, it is the wrong current order.

There are extensions for CORS, but the easiest is to present the page from the server.

POST not supported by http.server?

Answer
Date:
Correct:No

No teacher, I get the json data from the api on the internet like this and it works. Well the following is not the job to do after the request? (request.onload)

request.onload = function () {
          var data = JSON.parse(this.response)
          console.log(data);
 }

http server supports post method.

Answer
Date:
Correct:No

Sir, I ran my .json file on apache server and tried to access that. But there is nothing to say.

Answer
Date:
Correct:No

I need to allow something called Access-Control-Allow-Origin in the title to work (we mentioned CORS). But I couldn't find how to allow it in javascript.

Answer
Date:
Correct:No

Is there any server that does not have access to the file system?

Answer
Date:
Correct:No

First I downloaded an http server from npm with sudo npm install http-server -g command. Then I run this server with the cors parameter: http-server -p 3000 --cors. and it worked. Thank you very much for your support. But now there is a bigger problem. Writing data to json file located in local.

Answer
Date:
Correct:Yes

The javascript running in the browser cannot access the files of the device on which it is located. If your goal is to save files on the server, I guess a simple POST request would be enough.

Answer
Date:
Correct:No

You can do it on the back end.

Your Answer

Review Your Answer