开发者

Read a JSON file at localhost

开发者 https://www.devze.com 2023-03-28 12:57 出处:网络
I am trying to read a json file at localhost. it is not read by the browser. is this even possible? the json file is kept in my local pc.

I am trying to read a json file at localhost. it is not read by the browser. is this even possible? the json file is kept in my local pc.

 $.getJSON("p.json",function(data){
      alert("success");
     });

chrome gives me a 404 for this. the file is there. http://localhost/myfoldername/p.json

what is it that i am doing wrong? the json file has not been prepared by a server, but i have prepared it my self. the syntax is correct, though.

EDIT: if i shoot to the file in the browser by hitting its url, localhost/myfolder/p.json, it gives me a 404.3 error saying if it is a script file,开发者_开发问答 there should be a MIME handler for it. how to get this working?? – amit 0 secs ago edit


If the server says it is a 404, that means it cannot find it. 99% of the time this means that there is some problem with the request. Some thoughts:

  1. When you navigate to localhost/myfoldername/p.json do you get a 404? If so, that means that your path to the file is wrong. Make sure that myfoldername/p.json is in the appropriate directory on your hard drive (last night I lost a minute or so because I accidentally managed to save something to C:\xampp\php instead of C:\xampp\htdocs).
  2. If you got a 404 in the previous step and have confirmed that the file is in the right location, then you'll want to make sure that the file does not have any whitespace in its name (unlikely, but it does happen sometimes).
  3. p.json is a relative URL, is your current file in myfoldername? If not, the request will be to the wrong directory. Change "p.json" to myfoldername/p.json or ../myfoldername/p.json.

Edit

Just noticed your edit. I had not realized that IIS disabled the .json MIME type by default. You can find instructions on how to configure that here.


Is the file hosted by your webserver? If you are trying to access the file from File System, it is not possible as browsers restrict access to local drives. If the file is accessible through the URL (directly on browser with out jQuery) then jquery should be able to do and your code looks good.

0

精彩评论

暂无评论...
验证码 换一张
取 消