TypeError: invalid 'in' operand obj in jquery 1.9.1 version

后端 未结 7 2702
走了就别回头了
走了就别回头了 2021-02-20 16:25
$.ajax({
    async: false,
    type: \"POST\",
    url: url+\"module/listing/\"+projectId,
    data: \"ajax=true\",
    success: function(response) {
        $.each(resp         


        
相关标签:
7条回答
  • 2021-02-20 16:37

    I was getting this error while sending an array of column names to jQuery DataTables. The array needs to be an array of objects, not just an array of names. link to columns

    BAD columns: ["Column1", "Column2"]

    GOOD columns:[{title: "Column1"}, {title: "Column2"}]

    0 讨论(0)
  • 2021-02-20 16:38

    Use dataType: "json" to have jQuery parse the response as JSON. It will solve your problem.

    0 讨论(0)
  • 2021-02-20 16:42
    success: function(response) {
            response=JSON.parse(response);
            $.each(response, function(key, val) {
            alert(val.id);
            });
        }
    
    0 讨论(0)
  • 2021-02-20 16:51

    I had the same problem. The error is being triggered from the jQuery function 'isArraylike( obj )' from the following line:

    return type === "array" || type !== "function" &&
        ( length === 0 ||
        typeof length === "number" && length > 0 && ( length - 1 ) in obj );
    

    The Javascript 'in' operator needs an object as operand, so chances are if you do jQuery.type(response) it'll show something other than an object (eg. string or null)

    So Amit's answer should work - if not, check the type of the response data and work from there.

    0 讨论(0)
  • 2021-02-20 16:52

    Try this one, worked for me

    $.each(eval(response), function(key, val)
    

    For some reason (which I don't know), response is considered a string and not an object so you have to 'convert it' using eval().

    0 讨论(0)
  • 2021-02-20 16:57

    I got this error after I encoded twice a JSON array. Maybe it will help anybody.

    0 讨论(0)
提交回复
热议问题