Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my json file not found?

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>
like image 451
B. Clay Shannon-B. Crow Raven Avatar asked Jul 13 '13 03:07

B. Clay Shannon-B. Crow Raven


People also ask

Why is my JSON not working?

There are many reasons why you can't open a JSON file on your computer. In most cases, the issue could be that you are not using the right program to open a JSON file. If you don't have an application on your PC that is compatible with JSON files, it will be impossible to open one.

Where is my JSON file?

Open the Web store on your web browser using the apps option menu or directly using this link. Here, type JSON View in search bar under the Extensions category. You will get the various extensions similar to JSON View to open the JSON format files.

How do I restore a JSON file?

To import, open the bookmark Library, click the import/export button in the top bar, choose Restore → Choose File..., and then select your JSON backup file.


3 Answers

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>
like image 72
JAM Avatar answered Oct 23 '22 01:10

JAM


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>
like image 42
Kaushal Khamar Avatar answered Oct 23 '22 00:10

Kaushal Khamar


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

like image 30
Paul Milleson Avatar answered Oct 22 '22 23:10

Paul Milleson