Search code examples
jsonasp-classicjscript

'JSON' is null or not an object' error in classic ASP


I'm getting the error in the title --JSON is null or not an object.

I am inside a classic ASP page. My json2.js file is located in the same directory as this classic asp page that I'm in. So I have no idea why the server can't seem to recognize the JSON.parse() method. My scripting language is JScript.

I noticed that in a different thread, using the json2.js library is the way to go. But I can't seem to get this to work. Is this a quirk? This should be very straightforward, no?

requestBody is a simple JSON object: [{"answer":"ok"},{"answer":"sunny"}]

Thanks!

    <script language="javascript" runat="server" src="json2.js"></script>

<%
    var jsonObject = JSON.parse(requestBody);

%>

Solution

  • Not sure, but the first thing I would do is check the permissions on json2.js. Use icacls.exe, and verify that the permissions on that file match the perms on the .ASP page itself. I do this:

    %windir%\system32\icacls.exe json2.js /grant "NT AUTHORITY\IUSR:(RX)"
    %windir%\system32\icacls.exe json2.js /grant "BUILTIN\IIS_IUSRS:(RX)"
    

    Also - to help debug ASP you can fiddle with the ASP debugging settings in IIS Manager. Click "Send errors to client" to see the errors in the client browser window.

    Alternatively, you can manually add this to web.config:

    <configuration>
      <system.webServer>
        <httpErrors errorMode="Detailed" existingResponse="PassThrough" />
        <asp scriptErrorSentToBrowser="false" />
      </system.webServer>
    </configuration>
    

    On Vista/2008/Win7, you may also need to "unlock" the web.config stanza that permits this.

     %windir%\system32\inetsrv\appcmd.exe unlock config  -section:system.webServer/asp
    

    This won't fix the "JSON not an object" problem, but it will give you better error messages when failures occur.