为什么找不到我的 json 文件?

我在 asp.net 项目的 Content 文件夹中有一个 json 文件:

<projectName>
\Content
NBCCJr.json

以及密码:

$.getJSON('~/Content/NBCCJr.json', function (data) {
$.each(data, function(i, dataPoint) {
// Bla
});
});
)

... 但是当代码被调用时什么也没有发生; 浏览器控制台说,“加载资源失败: 服务器响应状态为404(未找到)”

为什么没有找到? “波浪打击文件名”是不是正确的路径到文件?

更新

我也试过用反向的“重击”:

$.getJSON('~\Content\NBCCJr.json', function (data) {

... 得到了同样的结果(“ 加载资源失败: 服务器响应状态为404(未找到)”)

更新2

然后我就这样试了一下,没有预先打一下:

$.getJSON('Content/NBCCJr.json', function (data) {

我在控制台收到一条模棱两可的信息:

*GET http://localhost:9702/Content/NBCCJr.json 404 (Not Found) jquery.js:8724
XHR finished loading: "http://localhost:9702/Content/NBCCJr.json".*

所以它没有被发现,但仍然装载?

更新3

当我试图通过更改以下内容来导航到浏览器中的文件时:

http://localhost:9702/Default.cshtml

... 到:

http://localhost:9702/Content/NBCCJr.json

我从文特 · 瑟夫,蒂姆 · 伯纳斯-李和/或阿尔 · 戈尔那里得到了一条信息丰富的 WSOD 消息:

HTTP 错误404.3-未找到 由于扩展配置的原因,无法为您请求的页面提供服务。如果页是脚本,则添加处理程序。如果文件应该被下载,添加一个 MIME 映射。

更新4

多亏了 JAM,它现在可以工作了。

我必须把它添加到 Web.Config:

  <system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
76824 次浏览

Have you tried removing the ~ ?

As in:

$.getJSON('/Content/dumboJr.json', function (data) {
$.each(data, function(i, dataPoint) {
// Bla
});
});
)

To allow the IIS to serve JSON files, try adding this to your web.config:

<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>

Try putting the *.json file in the webRoot, not in a sub folder. And then reference it like:

$.getJSON('NBCCJr.json', function (data) {

This of course requires the previous inclusion and instantiation of the jQuery system object from: jquery.min.js or the JSON structure from: json2-1.0.min.js

Solution is you need to add json file extension type in MIME Types

Method 1

Go to IIS, Select your application and Find MIME Types

enter image description here

Click on Add from Right panel

File Name Extension = .json

MIME Type = application/json

enter image description here

After adding .json file type in MIME Types, Restart IIS and try to access json file


Method 2

Go to web.config of that application and add this lines in it

 <system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>

I Changed .json to .txt and request is working fine. I am not sure about the consequence .txt can cause.

If you use ASP.NET Core, just put the file in wwwroot but if you use ASP.NET framework, this allows JSON extension from web.config as follows:

<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>

and

<location path="Content">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>