0

我正在通过 ajax 发送帖子数据,该数据如下所示:

var cardProperties = {
    container: $("#cardContainer"),
    elementsWrapper: $("#cardElementsWrapperBorder"),
    size: 0, //0 -> (90x50mm <-> 510px x 283px), 1-> (85x55mm <-> 482px x 312px)
    position: "h", //default horizontal
    bgImagesPath: "media/images/designs/backgrounds/", //path to card bg images
    floatingImagesPath: "media/images/designs/floating-images/", //path to card bg images
    imagesPerPage: 8,
    innerBorderMargins: 40, //this should be divided by 2 to get the actual margin
    currentCardSide: 1
};

所以基本上有一些常用数据,但是像containeror之类的字段elementWrapper可能包含很多关于该对象的信息,而且它也是子对象,所以这导致了我非常难看的错误Uncaught RangeError: Maximum call stack size exceeded,因为我不需要这两个字段我怎么能排除它从对象中删除,而不删除任何该信息,因为稍后我将在我的js脚本中需要这些东西。

编辑

这也是我的ajax代码:

$.post("server.php", {data: cardProperties},
   function(response){

   }
);
4

1 回答 1

1

删除函数和对象,您将只拥有这些字符串和数字:

var propertiesForAjax = (function(obj){
        var out = {};
        for(var i in obj){
           if(typeof obj[i]==='object' || typeof obj[i]==='function') continue;
           out[i] = obj[i];
        }
    })(cardProperties);

或者没有自执行功能:

function transformProps(obj){
   var out = {};
   for(var i in obj){
       if(typeof obj[i]==='function' || typeof obj[i]==='object') continue;
       out[i] = obj[i];
   }
}

var toPost = transformProps(cardProperties);

笔记:

  • 如果你正在转换一个对象,你需要尝试一些稍微不同的东西(例如检查一个有效对象的列表)
  • if you don't know what you're transforming, you probably shouldn't use this, as you could have unexpected output/lose things.
于 2012-07-10T21:45:04.543 回答