jQuery 为什么找不到我的 json 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17626776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is my json file not found?
提问by B. Clay Shannon
I have a json file in a Content folder within my asp.net project:
我的 asp.net 项目的 Content 文件夹中有一个 json 文件:
<projectName>
\Content
NBCCJr.json
...and the code to access it:
...以及访问它的代码:
$.getJSON('~/Content/NBCCJr.json', function (data) {
$.each(data, function(i, dataPoint) {
// Bla
});
});
)
...but nothing happens when the code is called; the browser console says, "Failed to load resource: the server responded with a status of 404 (Not Found)"
...但是当代码被调用时什么也没有发生;浏览器控制台显示“加载资源失败:服务器响应状态为 404(未找到)”
Why is it not found? Isn't "tilde whack filename" the correct route to the file?
为什么找不到?“波浪号重击文件名”不是文件的正确路径吗?
UPDATE
更新
I also tried it with the "whacks" backwards:
我还用“重击”向后尝试了它:
$.getJSON('~\Content\NBCCJr.json', function (data) {
...and got the same result ("Failed to load resource: the server responded with a status of 404 (Not Found)")
...并得到相同的结果(“加载资源失败:服务器响应状态为 404(未找到)”)
UPDATE 2
更新 2
Then I tried it sans a prepended whack thusly:
然后我尝试了它没有预先准备好的重击:
$.getJSON('Content/NBCCJr.json', function (data) {
...and I get this ambiguous message in the console:
...我在控制台中收到了这条模棱两可的消息:
*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?
所以它没有被找到并且仍然被加载?
UPDATE 3
更新 3
When I attempted to navigate to the file in the browser by changing:
当我尝试通过更改导航到浏览器中的文件时:
http://localhost:9702/Default.cshtml
...to:
...到:
http://localhost:9702/Content/NBCCJr.json
I got an informative WSOD message from Vint Cerf, Tim Berners-Lee, and/or Al Gore saying:
我收到了来自 Vint Cerf、Tim Berners-Lee 和/或 Al Gore 的信息丰富的 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.
HTTP 错误 404.3 - Not Found 由于扩展配置,无法提供您请求的页面。如果页面是脚本,则添加处理程序。如果应该下载文件,请添加 MIME 映射。
UPDATE 4
更新 4
Thanks to JAM, it is now working.
感谢 JAM,它现在可以工作了。
I had to add this to Web.Config:
我不得不将此添加到 Web.Config:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
回答by JAM
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:
要允许 IIS 提供 JSON 文件,请尝试将其添加到您的 web.config:
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
回答by Kaushal Khamar
Solution is you need to add json file extension type in MIME Types
解决方法是需要在MIME Types中添加json文件扩展名
Method 1
方法一
Go to IIS, Select your application and Find MIME Types
转到 IIS,选择您的应用程序并查找 MIME 类型
Click on Add from Right panel
单击右侧面板中的添加
File Name Extension = .json
文件扩展名 = .json
MIME Type = application/json
MIME 类型 = 应用程序/json
After adding .jsonfile type in MIME Types, Restart IISand try to access json file
在MIME Types 中添加.json文件类型后,重启 IIS并尝试访问 json 文件
Method 2
方法二
Go to web.config of that application and add this lines in it
转到该应用程序的 web.config 并在其中添加此行
<system.webServer>
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
</system.webServer>
回答by Paul Milleson
Try putting the *.json file in the webRoot, not in a sub folder. And then reference it like:
尝试将 *.json 文件放在 webRoot 中,而不是放在子文件夹中。然后像这样引用它:
$.getJSON('NBCCJr.json', function (data) {
This of course requires the previous inclusion and instantiation of the jQuery system object from: jquery.min.jsor the JSON structure from: json2-1.0.min.js
这当然需要jQuery的系统对象的从以前的夹杂物和实例:jquery.min.js从或JSON结构:json2-1.0.min.js
回答by Pankaj
I Changed .json to .txt and request is working fine. I am not sure about the consequence .txt can cause.
我将 .json 更改为 .txt 并且请求工作正常。我不确定 .txt 可能导致的后果。
回答by Moaz Salem
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:
如果您使用ASP.NET Core
,只需将文件放入wwwroot
但如果您使用ASP.NET
框架,这将允许来自 web.config 的 JSON 扩展,如下所示:
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
and
和
<location path="Content">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>