【问题标题】:Getting a "TypeError: invalid 'in' operand a" within WordPress在 WordPress 中获取“TypeError: invalid 'in'operand a”
【发布时间】:2015-11-04 15:20:48
【问题描述】:

所以我得到一个 TypeError: invalid 'in' operand a error inside WordPress(不确定这是否与它相关),我不确定代码有什么问题。

这里是有问题的代码:

e_jsonObj = [];            
$.each(the_wpcc_posts, function(i, the_wpcc_post) {
    var e_post_id = the_wpcc_post[i].ID;
    var e_title = the_wpcc_post[i].post_title;
    var e_start = the_wpcc_post[i].post_date_gmt;

    e_item = {}
    e_item ["id"] = e_post_id;
    e_item ["title"] = e_title;
    e_item ["start"] = e_start;

    console.log(e_item);

    e_jsonObj.push(e_item);         

});

非常感谢这里的任何帮助。

【问题讨论】:

    标签: javascript jquery json wordpress each


    【解决方案1】:

    通过在each循环之前解析数据来尝试这种方式。 似乎您正在尝试迭代一个字符串,这导致了这个错误。

    e_jsonObj = [];
    data = $.parseJSON(the_wpcc_posts);  // parsing data          
    $.each(data, function(i, the_wpcc_post) {
        var e_post_id = the_wpcc_post[i].ID;
        var e_title = the_wpcc_post[i].post_title;
        var e_start = the_wpcc_post[i].post_date_gmt;
    
        e_item = {}
        e_item ["id"] = e_post_id;
        e_item ["title"] = e_title;
        e_item ["start"] = e_start;
    
        console.log(e_item);
    
        e_jsonObj.push(e_item);         
    
    });
    

    【讨论】:

    • 感谢您的回答!然而,这似乎引起了另一个问题。这是我现在得到的错误:SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 1192 of the JSON data
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-12
    • 2016-11-13
    • 1970-01-01
    • 2019-11-10
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多