在localhost上读取JSON文件

时间:2021-10-06 22:44:17

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.

我试图在localhost读取一个json文件。它不是由浏览器读取的。这有可能吗? json文件保存在我的本地电脑中。

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

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

chrome为我提供了404。文件在那里。 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.

我做错了什么? json文件还没有由服务器准备,但我已经准备好了我自己。但是语法是正确的。

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

编辑:如果我通过点击它的url,localhost / myfolder / p.json来浏览浏览器中的文件,它会给我一个404.3错误,说如果它是一个脚本文件,应该有一个MIME处理程序。如何让这个工作? - amit 0秒前编辑

2 个解决方案

#1


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:

如果服务器说它是404,那意味着它找不到它。 99%的时间这意味着请求存在一些问题。一些想法:

  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. 当您导航到localhost / myfoldername / p.json时,您会得到404吗?如果是这样,那意味着您的文件路径是错误的。确保myfoldername / p.json位于硬盘驱动器上的相应目录中(昨晚我丢失了一分钟左右,因为我不小心设法将某些内容保存到C:\ xampp \ php而不是C:\ xampp \ htdocs)。

  3. 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).
  4. 如果您在上一步中获得了404并确认该文件位于正确的位置,那么您将需要确保该文件的名称中没有任何空格(不太可能,但有时会发生)。

  5. 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.
  6. p.json是一个相对URL,是myfoldername中的当前文件吗?如果没有,请求将到错误的目录。将“p.json”更改为myfoldername / p.json或../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.

刚注意到你的编辑我没有意识到IIS默认禁用了.json MIME类型。您可以在此处找到有关如何配置的说明。

#2


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.

该文件是由您的网络服务器托管的吗?如果您尝试从文件系统访问该文件,则无法通过浏览器限制对本地驱动器的访问。如果文件可以通过URL访问(直接在没有jQuery的浏览器上),那么jquery应该能够做到并且你的代码看起来很好。

#1


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:

如果服务器说它是404,那意味着它找不到它。 99%的时间这意味着请求存在一些问题。一些想法:

  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. 当您导航到localhost / myfoldername / p.json时,您会得到404吗?如果是这样,那意味着您的文件路径是错误的。确保myfoldername / p.json位于硬盘驱动器上的相应目录中(昨晚我丢失了一分钟左右,因为我不小心设法将某些内容保存到C:\ xampp \ php而不是C:\ xampp \ htdocs)。

  3. 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).
  4. 如果您在上一步中获得了404并确认该文件位于正确的位置,那么您将需要确保该文件的名称中没有任何空格(不太可能,但有时会发生)。

  5. 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.
  6. p.json是一个相对URL,是myfoldername中的当前文件吗?如果没有,请求将到错误的目录。将“p.json”更改为myfoldername / p.json或../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.

刚注意到你的编辑我没有意识到IIS默认禁用了.json MIME类型。您可以在此处找到有关如何配置的说明。

#2


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.

该文件是由您的网络服务器托管的吗?如果您尝试从文件系统访问该文件,则无法通过浏览器限制对本地驱动器的访问。如果文件可以通过URL访问(直接在没有jQuery的浏览器上),那么jquery应该能够做到并且你的代码看起来很好。