Javascript – ASP.Net AJAX JavaScript Serialization Error

asp.netasp.net-ajaxjavascript

Ran into an “Out of Stack Space” error trying to serialize an ASP.Net AJAX Array object.

Here is the scenario with simplified code:

  1. Default.aspx

  2. MainScript.js

    function getObject(){
        return new Array();
    }
    
    function function1(obj){
        var s=Sys.Serialization.JavaScriptSerializer.serialize(obj);
        alert(s);
    }
    
    function function2(){
        var obj=getObject();
        var s=Sys.Serialization.JavaScriptSerializer.serialize(obj);
        alert(s);
    }
    
  3. Content.aspx

  4. ContentScript.js

    function serializeObject(){
        var obj=window.top.getObject();
        window.top.function1(obj); // <– This works fine
    
        obj=new Array();
        window.top.function1(obj); // <– this causes an Out of Stack Space error
    }
    

The code for the sample pages and JavaScript is here.

Posting the code for the aspx pages here posed a problem. So please check the above link to see the code for the aspx pages.

A web page (default.aspx) with an IFrame on that hosts a content page (content.aspx).

Clicking the “Serialize Object” button calls the JavaScript function serializeObject(). The serialization works fine for Array objects created in the top window (outside the frame). However if the array object is created in the IFrame, serialization bombs with an out of stack space error. I stepped through ASP.Net AJAX JS files and what I discovered is, the process goes into an endless loop trying to figure out the type of the array object. Endless call to Number.IsInstanceOf and pretty soon you get an out of stack error.

Any ideas?

Best Solution

This problem happens because Sys.Serialization.JavaScriptSerializer can't serialize objects from others frames, but only those objects which where instantiated in the current window (which calls serialize() method). The only workaround which is known for me it's making clone of the object from other frame before calling serialize() method.

Example of the clone() methode you can find here (comments in Russian): link text