jquery在通过ajax发送时将数据排除在对象之外

jquery exclude data out of object when sending via ajax

本文关键字:排除 数据 对象 ajax jquery      更新时间:2023-09-26

我通过ajax发送post数据,该数据如下所示:

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
};

基本上有一些常见的数据,但像containerelementWrapper这样的字段可能包含了很多关于该对象及其子对象的信息,所以这导致了我非常丑陋的错误Uncaught RangeError: Maximum call stack size exceeded,因为我不需要这两个字段,我如何在不删除任何信息的情况下将其从对象中排除,因为我稍后在js脚本中需要这些信息。

编辑

这里还有我的ajax代码:

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

删除函数和对象,只会得到那些字符串和数字:

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);

注:

  • 如果你正在转换一个对象,你需要尝试一些稍微不同的东西(例如,对照有效对象列表进行检查)
  • 如果你不知道你在转换什么,你可能不应该使用这个,因为你可能会有意外的输出/丢失东西