JQUERY无法识别来自请求的JSON响应

时间:2018-11-04 14:57:03

标签: javascript jquery ajax

我正在尝试进行多张图像上传,我在控制选项卡网络中获得了响应,并且都可以正常上传,但是一旦我想在控制台中使用响应将其打印为“未定义”

我来自网络的回复:

{"msg":"<div class=\"alert alert-success\"><i class=\"fa fa-check\"><\/i> The file 5ac6060c8fc14 (1).jpeg successfuly uploaded.<\/div>","all_images":[{"name":"5ac6060c8fc14 (1).jpeg","dir_name":"f1197245ac6060c8fc14 (1).jpeg"},{"name":"5ac6060c8fc14_1_192x192.jpeg","dir_name":"abf70115ac6060c8fc14_1_192x192.jpeg"},{"name":"5ac6060c8fc14_1_192x192.jpeg","dir_name":"8b2f2625ac6060c8fc14_1_192x192.jpeg"},{"name":"5ac6060c8fc14_1_192x192.jpeg","dir_name":"bb2d7165ac6060c8fc14_1_192x192.jpeg"},{"name":"5ac6060c8fc14.jpeg","dir_name":"b1538ee5ac6060c8fc14.jpeg"},{"name":"5ac6060c8fc14_1_192x192.jpeg","dir_name":"f73d4195ac6060c8fc14_1_192x192.jpeg"},{"name":"5ac6060c8fc14_1_192x192.jpeg","dir_name":"6a2a2c35ac6060c8fc14_1_192x192.jpeg"},{"name":"5ac6060c8fc14.jpeg","dir_name":"bfb8ac35ac6060c8fc14.jpeg"}]}

我的代码:

$('#upload_files').easyupload({
        url: 'sources/requests/ajax.php?type=upload-files',
        on_upload_before: function (info) {
            $('#status_1_text').html('');
            $('#progress_1').fadeIn(150);
            $('#status_1,#status_1_total').css({width: 0});
        },
        on_progress: function (progress) {
            $('#status_1').css({width: progress.progress_file + '%'});
            $('#status_1_total').css({width: progress.progress_total + '%'});
            $('#status_1_text').html('Sending files ' + progress.current_file +
                            ' / ' + progress.total_files + ' ');
        },
        on_upload_file: function (data) {
            $('#status_1').css({width: 0});
            $('#status_1_text_total').html(data.msg)
            $('#gallery').html(data.all_images)
        },
        on_upload_finish: function (data) {
            console.log(data);
            $('#progress_1').fadeOut(500);
            $('#status_1_text_total').append('All files ' +
                            'successfully uploaded to the server.');
        }
    });

当我打印data.msg时显示它但图像不起作用时,即使我写console.log(data);其返回的undefined

1 个答案:

答案 0 :(得分:0)

嘿,您收到的响应都是有效的json响应,我什至看不到错误,甚至尝试在控制台中打印它,而且效果很好

console screenshot

如果仍然存在问题,可能与如何处理请求的响应形式有关,您可以将响应类型作为JSON提供并解析数据。

如果您仍然倾向于收到此错误请求,请在console.log上记录数据并检查所接收的值,并将其提供为您的问题的编辑内容,以帮助我们更好地理解问题。