json.js and AJAX

Last post 02-11-2008 5:38 PM by apijunkie. 8 replies.

Sort Posts:

  • json.js and AJAX

    02-19-2007, 12:44 PM
    • Member
      11 point Member
    • paultyng
    • Member since 07-21-2006, 6:50 PM
    • Posts 5

    Is there a way to get around the compatibilities between the JSON implementation on http://www.json.net (json.js) which is supposedly going to become part of the JavaScript programming language and ASP.NET AJAX's javascript libraries?

     json.js is a legacy requirement of an app I'm working on and I'd like to use AJAX as well but it doesn't seem like the two play nicely together...

     Thanks,

     Paul

    Filed under: , ,
  • Re: json.js and AJAX

    02-19-2007, 6:43 PM
    • Contributor
      7,416 point Contributor
    • Garbin
    • Member since 09-17-2004, 12:35 PM
    • Sassari, Italy
    • Posts 1,506
    • ASPInsiders
      TrustedFriends-MVPs

    Hi,

    which kind of errors are you getting?

    Alessandro Gallo | Blog | My book: ASP.NET AJAX In Action
  • Re: json.js and AJAX

    02-20-2007, 8:54 AM
    • Member
      11 point Member
    • paultyng
    • Member since 07-21-2006, 6:50 PM
    • Posts 5

    http://forums.asp.net/thread/1440390.aspx

     I found a similar problem on that thread, the problem is I can't use the methods that ASP.NET AJAX exposes for JSON because the json.js library is already built in to the 3rd party control I'm using.
     

  • Re: json.js and AJAX

    02-20-2007, 7:48 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    I just made a simple page with a ScriptManager and a reference to json.js, and it loads fine with no error.

    So for now, I can't reproduce your issue, so I can't help.  If you can provide some code that demonstrates the problem, then I might be able to help.

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: json.js and AJAX

    02-21-2007, 1:52 PM
    • Member
      11 point Member
    • paultyng
    • Member since 07-21-2006, 6:50 PM
    • Posts 5

    The specific error I get is: Microsoft JScript runtime error: 'Sys.Res.enumValueNotInteger' is null or not an object

    The error occurs in the ScriptResource.axd request for AJAX script.  I'm not sure what else to tell you for repro, I don't have any update panels, I don't have any AJAX code, just a ScriptManager (with enable partial rendering = "true") and the JSON.JS script referenced.  I have the release of AJAX.  If i comment out JSON.js, no errors, if I put it back, I get the above error.  This is just on my login page, I have only a Login control and some static HTML (and the ScriptManager).

     Line 2421 of MicrosoftAjax.jsif (typeof(val) !== 'number' || (val % 1) !== 0) throw Error.invalidOperation(Sys.Res.enumValueNotInteger);

     Here is my page source:

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="errortest.aspx.cs" Inherits="System_errortest" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
        <script type="text/javascript" src="../3p/json/json.js"></script>
    </head>
    <body>
        <form id="form1" runat="server">
        <asp:ScriptManager ID="sm" runat="server" EnablePartialRendering="true" />
        </form>
    </body>
    </html>
    
     

  • Re: json.js and AJAX

    02-21-2007, 4:11 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    Strange, that's almost exactly the same code I tried.  Let me play some more with this and see if I can repro.

    Just to be sure, we're talking about http://json.org/json.js, right?

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: json.js and AJAX

    02-21-2007, 4:14 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    Ah!  I was running with debug="false", but I do get an error if I use debug="true".

    Investigating a little further...

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: json.js and AJAX

    02-21-2007, 4:36 PM
    Answer
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 8:35 PM
    • Microsoft
    • Posts 643

    Okay, the trouble is that json.js extends Object itself (a common cause of conflicts between AJAX libraries), so everything gets the .toJSONString() method attached.  This is a problem when the Microsoft AJAX Library tries to register its enumerations.  Type.registerEnum() tries to verify that the only values inside the enum are numbers, but the one called 'toJSONString' is a function, so it throws.

    One solution is to include json.js after including MicrosoftAjax.js.  The best way to do that would be to register the script with the ScriptManager, like:

            <asp:ScriptManager ID="ScriptManager1" runat="server">
                <Scripts>
                    <asp:ScriptReference Path="json.js" />
                </Scripts>
            </asp:ScriptManager>
    

    This is probably not an option for you, because json.js is getting included by another control that you can't change.

    Another option is to edit json.js (probably not possible if it's embedded in the control you're using?) or undo the change json.js makes to Object, like this:

        <script type="text/javascript" src="json.js"></script>
        <script type="text/javascript">
            delete Object.prototype.toJSONString;
        </script>

    This assumes that the extension of the actual Object prototype isn't needed.  (It already also extends Array, Boolean, Date, Number, and String.  Maybe that's sufficient for what it's being used for.)

    The last idea I have would be to modify MicrosoftAjax.js itself.  You can do that by copying down the Microsoft AJAX Library from ajax.asp.net, and then pointing the ScriptManager to your version via the ScriptPath attribute.  Then you can modify the scripts as you like.  Specifically, you'd have to at least comment out lines 2421 and 2422 of MicrosoftAjax.debug.js (and/or the corresponding lines of MicrosoftAjax.js).

    Note that this doesn't seem to be an issue when debug="false", so maybe you can get by without having to do one of these changes.

     

    I hope that helps, and sorry I don't have a better answer for you.

    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
  • Re: json.js and AJAX

    02-11-2008, 5:38 PM
    • Member
      2 point Member
    • apijunkie
    • Member since 11-27-2007, 7:29 AM
    • Posts 1

     

    Hi Steve,

    Thank you for the info.

    One thing I would like to add is that if you are using master pages or need json.js script inside custom controls you can use the script manager proxy Scripts property instead of the script manager Scripts property.

    Trackback

    Filed under:
Page 1 of 1 (9 items)