【问题标题】:JSON.stringify() doesn't render the object that is changed by an eventJSON.stringify() 不渲染被事件改变的对象
【发布时间】:2016-12-19 09:00:07
【问题描述】:

这是我的代码:

//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 对象恢复为默认值。特此附上它的截图:

没有Stringifyconsole.log(userData);,(正确的那个):

使用 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 恢复到默认顺序。

【问题讨论】:

  • 这里没有足够的代码来找到您的错误或了解您的目标。
  • 成功回调会发生什么?

标签: javascript jquery json ajax object


【解决方案1】:

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

【讨论】:

  • 你的意思是console.clear() ...这一切都很好,但是Note that in Google Chrome, console.clear() has no effect if the user has selected "Preserve log upon navigation" in the settings.
  • 您也可以点击CTRL + L。比输入console.clear()方便多了
  • 在我看来 Yusril Maulidan Raji 想以编程方式进行
  • @Raulucco 但是,正如您在我的代码中看到的那样,我将console.log(JSON.stringify(userData)); 放在console.log(userData); 之后。这两个应该是一样的。然而,就我而言,不知何故,这些是不同的。
  • @YusrilMaulidanRaji 正如我所说,您必须再次调用它。因为在调用 ajax 函数之前,对象与成功回调中的对象不同。您需要再次显式调用它才能看到新状态。这是因为您进行了函数调用并打印了一个非动态值(字符串)。
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 2020-07-05
  • 2016-03-08
  • 2021-05-01
  • 2022-07-11
  • 2021-08-09
  • 2018-04-04
  • 1970-01-01
相关资源
最近更新 更多