86

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

<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)"

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)")

UPDATE 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

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:

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.

UPDATE 4

Thanks to JAM, it is now working.

I had to add this to Web.Config:

  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json" />
    </staticContent>
  </system.webServer>
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

5 Answers5

97

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>
JAM
  • 6,045
  • 1
  • 32
  • 48
  • Yes (now I have). This: $.getJSON('/Content/NBCCJr.json', function (data) { gives the same response. – B. Clay Shannon-B. Crow Raven Jul 13 '13 at 14:35
  • Is it a 404 response? Have you tried navigating to the file directly in your browser? Is the Content folder in the root of your web project? – JAM Jul 13 '13 at 14:39
  • Yes, the post says that. Please see my updates. Yes, Content is a first-level folder, as indicated in the original post. – B. Clay Shannon-B. Crow Raven Jul 13 '13 at 14:42
  • Take a look at [this post](http://stackoverflow.com/questions/8158193/how-to-allow-download-of-json-file-with-asp-net). – JAM Jul 13 '13 at 14:46
  • If you add the specified web.config mime type you will be fine :) – JAM Jul 13 '13 at 14:51
  • If you add this via the iis management console, be sure to restart your application to see the changes. – Phil Cooper Apr 28 '14 at 16:15
  • Thanks! also check this for web.config ref... http://www.iis.net/configreference/system.webserver/staticcontent – Red Jul 22 '14 at 04:33
  • When I disable CORS of the browser and configure the Web.config like @Stefan is recommending on [this post](http://stackoverflow.com/a/19517275/1732989) everything is working for me – Kosmonaft Nov 21 '14 at 22:48
22

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>
Community
  • 1
  • 1
Kaushal Khamar
  • 2,097
  • 1
  • 17
  • 23
2

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

Navnath Godse
  • 2,233
  • 2
  • 23
  • 32
1

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

Pankaj
  • 401
  • 1
  • 6
  • 16
1

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>
Dale K
  • 25,246
  • 15
  • 42
  • 71
Moaz Salem
  • 438
  • 3
  • 5