0

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

amit
  • 10,133
  • 22
  • 72
  • 121

2 Answers2

1

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.

Community
  • 1
  • 1
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • thanks for your reply. but all three of the points dont hold good. the file is in the perfect location and i am pointing to it correctly. some other issue is acting up. – amit Aug 17 '11 at 12:48
  • What directory on the HD is your p.json in? What directory is your jQuery code in? – cwallenpoole Aug 17 '11 at 13:10
0

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.

Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
  • the file is located on the webserver. and i am running the page through the server. – amit Aug 17 '11 at 12:36
  • 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 Aug 17 '11 at 12:40