简体   繁体   中英

Getting error when getting data from json

I Have a Jquery Drop Upload form all working good but when i'm trying to get data from database with json its working but i don't know why its showing error. please check below for more infromation

my error

jQuery-2.1.4.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in [{"name":"1.xlsx","size":34838},{"name":"485138_1.xlsx","size":34838}]
at s (jQuery-2.1.4.min.js:2)
at Function.each (jQuery-2.1.4.min.js:2)
at Object.success (misupload.js:111)
at j (jQuery-2.1.4.min.js:2)
at Object.fireWith [as resolveWith] (jQuery-2.1.4.min.js:2)
at x (jQuery-2.1.4.min.js:4)
at XMLHttpRequest.<anonymous> (jQuery-2.1.4.min.js:4)

Here is my js code

$.get('/resumes/mis-upload-get-action?_act=bulk', function(data) {
    $.each(data, function(key,value){
        var mockFile = { name: value.name, size: value.size};
        thisDropzone.options.addedfile.call(thisDropzone, mockFile);
        totalFiles += 1;
        completeFiles += 1;
    });
    $('.btn-proceed .badge').html(totalFiles);
});

Here is my server side json code

[{"name":"1.xlsx","size":34838},{"name":"485138_1.xlsx","size":34838}]

Please Help me OUT

That error means jQuery is trying to iterate over something that it can't iterate over - in this case a string. You need to parse your data in order for jQuery to iterate over it.

You can do this either by using $.getJSON (jQuery will automatically parse your data to an object), or by using JSON.parse(data) .

try using .emit()

$.get('/resumes/mis-upload-get-action?_act=bulk', function(data) {
    $.each(data, function(key,value){
        var mockFile = { name: value.name, size: value.size};
        thisDropzone.emit("addedfile", mockFile);
        thisDropzone.emit("complete", mockFile);
        totalFiles += 1;
        completeFiles += 1;
    });
    $('.btn-proceed .badge').html(totalFiles);
});

Let me know if it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM