javascript 使用 jquery 在文件上传后解析 JSON 响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18104202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:44:25  来源:igfitidea点击:

Parsing a JSON response after a file upload with jquery

javascriptjquery

提问by G A

I have written a small script using jquery to upload a file to a server. The file is uploaded successfully and the done: event is called with no problems, but I am having issues to process the answer. This is my script:

我已经使用 jquery 编写了一个小脚本将文件上传到服务器。文件已成功上传并且 done: 事件被调用没有问题,但我在处理答案时遇到问题。这是我的脚本:

    <input id="fileupload" type="file" name="carPicture" accept="image/*" multiple>
    <div id="progress">
        <div class="bar" style="width: 0%;"></div>
    </div>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"></script>
    <script src="@routes.Assets.at("javascripts/vendor/jquery.ui.widget.js")" type="text/javascript"></script>
    <script src="@routes.Assets.at("javascripts/jquery.iframe-transport.js")" type="text/javascript"></script>
    <script src="@routes.Assets.at("javascripts/jquery.fileupload.js")" type="text/javascript"></script>
    <script>
        $(function () {
            'use strict';
            // Change this to the location of your server-side upload handler:
            var url = "uploadCarPicture";
            $('#fileupload').fileupload({
                url: url,
                dataType: 'json',
                done: function (e, data) {
                    $.each(data.result.files, function (index, file) {
                        $('<p/>').text(file.name).appendTo('#files');
                    });
                },
                fail: function (e, data) {
                    alert("File exists");
                    },
                progressall: function (e, data) {
                    var progress = parseInt(data.loaded / data.total * 100, 10);
                    $('#progress .bar').css(
                            'width',
                            progress + '%'
                    );
                }
            }).prop('disabled', !$.support.fileInput)
                    .parent().addClass($.support.fileInput ? undefined : 'disabled');
        });
    </script>

I am having two problems:

我有两个问题:

  1. the variable data seems to be empty since the loop below doesn't run even once.

            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo('#files');
            });
    
  2. The answer is a JSON document with the following format: {"e":0} where "e" is an error code. "e" could return many different values and I would like to be able to find out the real response and not always assume 0.

  1. 变量 data 似乎是空的,因为下面的循环甚至没有运行一次。

            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo('#files');
            });
    
  2. 答案是具有以下格式的 JSON 文档:{"e":0} 其中 "e" 是错误代码。“e”可以返回许多不同的值,我希望能够找出真正的响应,而不是总是假设为 0。

Any idea?

任何的想法?

回答by G A

I have solved it. I've made a small change in the java script like this:

我已经解决了。我在 java 脚本中做了一个小改动,如下所示:

done: function (e, data) {
      $.each(data.files, function (index, file) {
          $('<p/>').text(file.name);
        });
      },

And I have changed the json that the server was responding to this format:

我已经更改了服务器响应此格式的 json:

{ files: [ { error: 0, name: "thumb2.jpg", } ] }

{文件:[{错误:0,名称:“thumb2.jpg”,}]}