简体   繁体   English

ASP.Net AJAX JavaScript序列化错误

[英]ASP.Net AJAX JavaScript Serialization Error

Ran into an “Out of Stack Space” error trying to serialize an ASP.Net AJAX Array object. 遇到“ Out of Stack Space”错误,试图序列化ASP.Net AJAX Array对象。

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 . 示例页面和JavaScript的代码在此处

Posting the code for the aspx pages here posed a problem. 在此处发布aspx页面的代码带来了一个问题。 So please check the above link to see the code for the aspx pages. 因此,请检查上面的链接以查看aspx页面的代码。

A web page (default.aspx) with an IFrame on that hosts a content page (content.aspx). 带有IFrame的网页(default.aspx)托管内容页面(content.aspx)。

Clicking the “Serialize Object” button calls the JavaScript function serializeObject(). 单击“序列化对象”按钮,将调用JavaScript函数serializeObject()。 The serialization works fine for Array objects created in the top window (outside the frame). 对于在顶部窗口(框架外部)中创建的Array对象,序列化工作正常。 However if the array object is created in the IFrame, serialization bombs with an out of stack space error. 但是,如果在IFrame中创建数组对象,则序列化炸弹会出现堆栈空间不足错误。 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. 我逐步浏览了ASP.Net AJAX JS文件,发现的是,该过程陷入无休止的循环,试图找出数组对象的类型。 Endless call to Number.IsInstanceOf and pretty soon you get an out of stack error. 无休止地调用Number.IsInstanceOf,很快您就会收到堆栈错误。

Any ideas? 有任何想法吗?

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). 发生此问题的原因是Sys.Serialization.JavaScriptSerializer不能序列化其他框架中的对象,而只能序列化在当前窗口中实例化的那些对象(调用serialize()方法)。 The only workaround which is known for me it's making clone of the object from other frame before calling serialize() method. 对我而言,唯一已知的解决方法是在调用serialize()方法之前从其他框架克隆对象。

Example of the clone() methode you can find here (comments in Russian): link text 您可以在此处找到clone()方法的示例(俄语注释): 链接文本

I converted your example to a set of static html files, and dowloaded the standalone Microsoft Ajax Library 3.5 to test with. 我将您的示例转换为一组静态html文件,并下载了独立的Microsoft Ajax Library 3.5进行测试。 It didn't have issue on either Firefox 3 or IE 7, but I did notice the first alert box displayed [] (an array) and the second {} (an object). 在Firefox 3或IE 7上都没有问题,但是我确实注意到显示的第一个警报框[](数组)和第二个{}(对象)。

Then, I converted your new Array() code to: 然后,我将您的新Array()代码转换为:

  var obj = [];
  obj.push(1);

and after that, I got [1] and {"0", 1} is the alert boxes. 然后,我得到了[1]和{“ 0”,1}是警报框。 I don't think the bug is with JavaScriptSerializer, but something to do with passing objects across frames. 我不认为该错误与JavaScriptSerializer有关,而是与跨帧传递对象有关。

I have no way of testing your code right now, but it looks like a bug in JavaScriptSerializer.serialize to me. 我目前无法测试您的代码,但是对我来说,它看起来像是JavaScriptSerializer.serialize中的错误。 My guess is that it tries to do some kind of type checking on the array via the CLR and that it doesn't handle an empty array properly. 我的猜测是,它尝试通过CLR对数组进行某种类型检查,并且不能正确处理空数组。

Have you tried to add an item of a serializable type to the array in your code? 您是否尝试过将可序列化类型的项添加到代码中的数组? If so, what happens? 如果是这样,会发生什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM