JSON.stringify()不会呈现由事件更改的对象

时间:2016-12-19 09:00:07

标签: javascript jquery json ajax object

这是我的代码:

//column reorder event
dataTable.on('column-reorder', function (e, settings, details) {
    var userData = tableWidget.grid('userData');
    console.log(userData); //the object which is correct already
    console.log(JSON.stringify(userData)); //somehow it's different than without stringify
    $.ajax({
        url: "../../server/post.aspx?tableEvent=reordercolumns&table=SubContractor",
        data: { dataColumnOrder: userData },
        dataType: "json",
        type: "POST",
        success: function (response) {
            //write JS when ajax call success
            console.log('success');
        },
        error: function () {
            console.log("Something wrong with the columns' reorder saving process");
        }
    });
    console.log("Reordered");
});

此事件将更新userData对象。更新部分工作正常。不幸的是,当我对其进行字符串化时,userData对象将恢复为默认值。我附上了它的截图:

没有Stringify console.log(userData);,(正确的):

enter image description here

使用Stringify console.log(JSON.stringify(userData));

{
    "view": "SubContractorGridView",
    "title": "SubContractor Grid View",
    "settings": {
        "colOrder": ["Id", "ExtKey", "InsertionDate", "DeletionDate", "Name", "Address", "ZipCode", "EmailAddress", "PhoneNumber", "UserName", "Password", "LastUpdate", "Price"],
        "orderBy": "Name",
        "orderDir": "asc"
    },
    "columns": [{
            "dbField": "Id",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 89,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "ExtKey",
            "show": false,
            "widthMobile": 90,
            "widthPhablet": 120,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 150,
            "widthDesktop": 90,
            "widthLargeDesktop": 100
        }, {
            "dbField": "InsertionDate",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 100,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 100,
            "widthDesktop": 90,
            "widthLargeDesktop": 110
        }, {
            "dbField": "DeletionDate",
            "show": false,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 100
        }, {
            "dbField": "Name",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 130,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "Address",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "ZipCode",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "EmailAddress",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "PhoneNumber",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "UserName",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "Password",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "LastUpdate",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }, {
            "dbField": "Price",
            "show": true,
            "widthMobile": 90,
            "widthPhablet": 90,
            "widthMiniTablet": 90,
            "widthTablet": 90,
            "widthSmallDesktop": 90,
            "widthDesktop": 90,
            "widthLargeDesktop": 90
        }
    ]
}

正如您所看到的那样,该列的顺序会以某种方式返回到使用stringify的默认顺序。

1 个答案:

答案 0 :(得分:0)

字符串不是动态的,一旦你创建字符串并在控制台中打印它就不会改变。 但是对象是动态的,因此当对象属性发生更改时,您会在控制台上看到更新值,因为它会连接对象的评估。 如果要显示更新的值,则必须再次调用JSON.stringify。 如果您对使用console.clear()的旧值不感兴趣,可以清理控制台 有关console on chrome

的更多信息