【问题标题】:How to debug circular reference that JSON.stringify is reporting如何调试 JSON.stringify 报告的循环引用
【发布时间】:2019-05-03 20:13:28
【问题描述】:

调用 JSON stringify 时出现循环引用异常,但找不到循环引用。看来 jQuery 是这里的罪魁祸首,但我没有看到问题,也无法进入 JSON 字符串化。

const list = $('.use-in-reporting-checkbox:checkbox:checked').map(function() 
{
    return this.value;
});

const dataPacket = {
    datasetIDs: list
};

try {
    const real = JSON.stringify(dataPacket);

} catch (error) {
    processError(error);
}

"Error reports: Converting circular structure to JSON
    --> starting at object with constructor 'Object'
    property 'selectorData' -> object with constructor 'Object'
    |     property 'elements' -> object with constructor 'Array'
    --- index 0 closes the circle"

But, inspection of dataPacket just shows: "datasetIDs init (37)" with the 
list of checkbox values. Not sure how to debug this.

【问题讨论】:

    标签: jquery arrays json ecmascript-5


    【解决方案1】:

    发生此错误是因为您获取的是 jQuery 对象而不是所需的值。

    .map-method 返回 jQuery-object,应该由 get-call 解决:

    const ids = $('.use-in-reporting-checkbox:checkbox:checked').map(function() 
    {
        return this.id;
    }).get();
    

    另一种方式:

    const ids = jQuery.map($('.use-in-reporting-checkbox:checkbox:checked'), function(v) 
    {
        return v.id;
    });
    

    【讨论】:

    • 有道理。我将在星期一对此进行测试。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多