Saturday, April 8, 2017

$.getJSON() not loading JSON file within Visual Studio Project in ASP.NET WebForms

Hi,
I was trying to load a JSON file located within my Visual Studio project using $.getJSON(), however the code below doesn't work as expected.
$.getJSON('/Assets/misc/employee.json', function (data) {
 alert('processing!');
})
.done(function (r) {
 alert(r.message);
})
.fail(function (s) {
 alert('oops the file is missing!');
});
After investigating for a few hours, I tested the local path of the JSON file such as http://localhost:3766/Assets/misc/employee.json and the result was an exception "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.".
I remember the solution in one of my ASP.NET project was to set the MimeMap in the web.config inside the system.webServer element. After setting the MimeMap for json files, I can now load local JSON files using $.getJSON().
<system.webServer>
 <staticContent>
   <mimeMap fileExtension=".json" mimeType="application/json" />
 </staticContent>
</system.webServer>

:-)

0 comments:

Post a Comment