Tip – Serving .json File on Windows (IIS, IIS Express)

imageSo what is wrong with the simple getJSON call? Why doesn’t it work?

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
  $(document).ready(function(){
     $.getJSON('data.json',function(result){
      alert("success");
    });
  });

It works fine in Firefox 11 but not in IE and Chrome. By default, IIS6 does not serve .json (no wildcard MIME type). So you will see a 404 not found thrown.

By default, IIS in Windows Server 2003 and beyond does not serve files that aren’t of a MIME type that it knows about (instead returning 404 errors).

So, to serve up JSON files you need to add a MIME type to IIS to allow it to serve that type of file. You can set it at the site level or at the server level.

Remedy 1. Rename JSON File

When you are working on Windows in Visual Studio, you are often using the local IIS Web Server. IIS doesn’t support JSON by default, so you can try renaming your jdata.json file to data.txt or data.html  So your $.getJSON() becomes to $.getJSON('data.html,...

NOTE: This is the solution you will find in most of the sample code in our repository on GitHub. That is because it is much simpler to deploy to users who have not configured their server for the JSON file type.

Remedy 2. Adding MIME Type to IIS Express

IIS Express is a lightweight, self-contained version of IIS optimized for developers. It is what you are probably running IIS Express makes it easy to use the most current version of IIS to develop and test websites. It has all the core capabilities of IIS 7 and above as well as additional features designed to ease website development including:

  • It doesn’t run as a service or require administrator user rights to perform most tasks.
  • IIS Express works well with ASP.NET and PHP applications.
  • Multiple users of IIS Express can work independently on the same computer.

In fact, the default setting for your project is Use Local IS Web server, with Use IIS Express checked.

Right-click your project, click Properties, click Web to see this view, scroll to see the Servers section.

image

Application MIME types denote which application should interpret data in a file instead of displaying raw data on a web page. Modern web browsers handle many application files directly with plugins, including JavaScript, Microsoft Office and PDF documents, RSS feeds, Java code, Adobe Flash and Shockwave, and Microsoft Silverlight files. Users can choose other software to handle application files instead.

When you request a file from an IIS 6.0 Web server, and the file has a file name extension that is not a defined MIME type on the Web server, you receive the following error message:

HTTP Error 404 – File or directory not found.

IANA has registered the official MIME Type for JSON as application/json.

See here:http://www.iana.org/assignments/media-types/application/

But neither IIS nor IIS Express has .json registered. So you will get a 404 error.

Fix for .json files who get “Failed to load resource: the server responded with a status of 404 (Not Found)”

You can add a MIME Type using the command line using the following syntax:

appcmd set config /section:staticContent /+”[fileExtension=’string ‘,mimeType=’string ‘]”

Open up a command prompt with administrative privileges.

Navigation to the IIS Express directory. This lives under Program Files or Program Files (x86). Type:

cd c:\Program Files (x86)\IIS Express
appcmd set config /section:staticContent /+”[fileExtension=’.json‘,mimeType=’application/json‘]”

NOTE: spaces count

You will receive a response back from your Web server.

image

To see the settings:

appcmd list config /section:staticContent

Your new MIME Type will be at the bottom of the list.

Remedy 3. Running in IIS in Production

The answer will often depend on whether you have access to the server you want to deploy to. That is because you want to be prepared to install your application on your deployment server.

For example, if you do not have access to the IIS console on the deployment server, you may just want to stick with the file naming convention.

Otherwise, you can enable .json file extension in IIS.  Here’s how.

  1. Search for an app named Internet Information Services (IIS) manager.

    NOTE: If you do not have it, you will need to install it. Use Microsoft Web Platform Installer. The Microsoft Web Platform Installer (Web PI) is a free tool that makes getting the latest components of the Microsoft Web Platform, including Internet Information Services (IIS), SQL Server Express, .NET Framework and Visual Web Developer easy. The Web PI also makes it easy to install and run the most popular free web applications for blogging, content management and more with the built-in Windows Web Application Gallery.

  2. When you start IIS Manager, double click MIME Types in the center panel.
    image
  3. Right-click the MIME Types panel. Click Add.
    image
  4. Type json for the extension and application/json for the MIME type.
    image
    Click OK.

When you run your application in IIS, your app will respond as expected.

Scripting MIME Type for IIS

To prepare a script for your deployment server, see Add a MIME Type (IIS 7). You can use the previous section as a template. But instead of starting in IIS Express, you will start is IIS.

 

cd c:\Program Files (x86)\IIS
appcmd set config /section:staticContent /+”[fileExtension=’ .json ‘,mimeType=’ application/json‘]”

Other MIME Types

While researching this, I found that several MIME Types that you may need for HTML5 development should work. MP4 and SVG are already configured. But if you need to, you can follow the same procedures in the previous sections to add your own MIME Types.

Extra Points

So what does MIME stand for?

Multipurpose Internet Mail Extensions (MIME)

References


					
Advertisements