Question

I have written an html file using jQuery to read a JSON file. It works fine by itself; I tested it using Safari. However, when I try to use the exact same code in WebMatrix, it gives me an error, saying the JSON file cannot be found. I checked and the file is there.

Here is a simple version of the offending code. (The same comments apply: it works by itself in Safari but says "file not found" when used in WebMatrix.)

Can someone help me?

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"> </script>
<script>
    var Email=new Array();
    var i=0;
    var email;

    $.getJSON('MoodMinder.json', function(data) {
      $.each(data.User, function(i, f) {
        Email[i]=f.Email;
        i++;
      })
      email="<p>" + Email[0] + "</p>";
      $(email).appendTo("#email");
    });
</script>
</head>

<body>
  <div id="email"></div>  
</body>
</html>

Here is the JSON:

{
    "User":[
        {"Email":"cklann999@gmail.com"}
    ]
}

And here is the Web.config file (added section recommended by Nidzix):

<?xml version="1.0" encoding="utf-8"?>

<configuration>
    <system.web>
        <customErrors mode="Off"></customErrors>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add invariant="System.Data.SqlServerCe.4.0" name="Microsoft® SQL Server® Compact 4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".json" mimeType="application/json"></mimeMap>
        </staticContent>
    </system.webServer>
</configuration>
Was it helpful?

Solution

You have to have configuration file called Web.config in the root folder, containing this piece fo code:

<?xml version="1.0"?>

<configuration>

    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".json" mimeType="application/json" />
        </staticContent>
    </system.webServer>

</configuration> 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top