I have a json file in a Content folder within my asp.net project:

<projectName>
    \Content
        NBCCJr.json

.以及访问它的代码:

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

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

为什么找不到?"tilde-whack-filename"不是文件的正确路径吗?

使现代化

我也试过用"重击"倒过来:

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

.并得到相同的结果("Failed to load resource: the server responded with a status of 404 (Not Found)")

使现代化 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".*

So it was not found and yet loaded anyway?

使现代化 3

When I attempted to navigate to the file in the browser by changing:

http://localhost:9702/Default.cshtml

...致:

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

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

HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

使现代化 4

Thanks to JAM, it is now working.

我不得不把这个添加到网络上.配置:

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

推荐答案

Have you tried removing the ~ ?

例如:

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

要允许IIS提供JSON文件,请try 将其添加到web.配置:

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

Jquery相关问答推荐

更改高亮 colored颜色

如何在 JavaScript 或 jQuery 中过滤 JSON 数据?

jQuery 的元素或类 LIKE Select 器?

在页面上放置文件时如何设置文件输入值?

如何设置缓存:jQuery.get 调用中的 false

使用 jQuery 从 JavaScript 对象中添加/删除项目

如何在实际图像下载时显示加载图像

删除索引后的所有项目

使用 JQuery 获取触发事件的元素的类

如果不是 jQuery,Javascript 中的美元符号是什么

Jquery:如何判断元素是否具有某些 css 类/样式

Bootstrap 4 文件输入

jQuery表格行中的每个循环

获取没有在css中设置高度的div的高度

如何从所有元素jquery中删除类

从客户端的对象数组中获取最新日期的优雅方法是什么?

jQuery clone() 不克隆事件绑定,即使使用 on()

如何删除集中的 contenteditable pre 周围的边框?

CORS - 如何预检httprequest?

自调用函数前的分号?