For example, in an external JavaScript file I have this inside a simple $(document).ready function:
$.getJSON("/aTest.json", function (jsonObj) {
$("#testJSONBtn").click(function () {
var val = "";
for (var i = 0; i <= jsonObj.events.length; ++i) {
val += jsonObj.events[i].title + ", " + jsonObj.events[i].date + ", " + jsonObj.events[i].explanation + "<br/>";
}
$("#JSONOutput").append(val);
});
});
JavaScript console keeps telling me that the server returned 404 (not found), however the file is definitely exactly that name, and exactly on that path. All other resources are accessed by the site just fine. I have tried moving the file around and renaming it, and a slew of other things, so I get the feeling that maybe this has something to do with WebMatrix, the version of IIS that its running (is it 8?), or the file extension I am using (.json). Aren’t external json files supposed to be stored in a file with the .json extension?
Additional Peculiarity:
I once had two files with the same name, one was the .json file and the other was.cshtml, and they were in the same directory. During this time the above function didn’t return a 404, in fact, it returned nothing at all… It did nothing, but it returned no errors.
If it matters, all ids and selectors have been quintuple checked (and beyond).
Is there something wrong with the above function somehow, or will an external .js file (or WebMatrix’s IIS) not access another external .json file like this?
It sounds like you need to add a static mapping for *.json files in your applicationHost.config. This post does a nice job explaining how to add a mime type to IIS Express:
http://www.tomasmcguinness.com/2011/07/06/adding-support-for-svg-to-iis-express/
You can also check out the IIS Express logs to see if there are more details in the log. They’re usually located at ~\Documents\IISExpress\Logs
Happy Coding!