Read a JSON file at localhost
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:
- When you navigate to
localhost/myfoldername/p.jsondo you get a 404? If so, that means that your path to the file is wrong. Make sure thatmyfoldername/p.jsonis in the appropriate directory on your hard drive (last night I lost a minute or so because I accidentally managed to save something toC:\xampp\phpinstead ofC:\xampp\htdocs). - 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).
- 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" tomyfoldername/p.jsonor../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.
加载中,请稍侯......
精彩评论