【问题标题】:dropzone.js redirect after success not working from json returned by php codeigniter成功后dropzone.js重定向无法从php codeigniter返回的json工作
【发布时间】:2018-02-11 02:34:24
【问题描述】:

我尝试在我的 dropzone 上传成功后使用来自控制器的 echo json_encode 中的 $.getJSON 进行重定向,但似乎我的 javascript 实际上并没有获取 json_encode,因为我尝试在 $.getJSON 中添加警报

这是我的 dropzone javascript

Dropzone.options.myAwesomeDropzone = {
      url: "<?php echo(base_url('upload/image')); ?>",
      addRemoveLinks: true,
      autoProcessQueue: false,
      autoDiscover: false,
      maxFilesize: 5,
      maxFiles: 1,
      acceptedFiles: ".png,.jpg,.gif",
      previewsContainer: '#dropzonePreview',
      clickable: "#dropzonePreview",
      accept: function(file, done) {
        console.log("uploaded");
        done();
      },
      init: function() {
          var myDropzone = this;
        //now we will submit the form when the button is clicked
            this.on("addedfile", function(file) { 
                $('.dz-message').hide();
                $('#dropzonenoimageerrormessage').hide();
            });
            this.on("reset", function(file) { 
                $('.dz-message').show();
            });
            this.on("maxfilesexceeded", function(file) {
                this.removeAllFiles();
                this.addFile(file);
            });
        $("#sbmtbtn").on('click',function(e) {
            // Make sure that the form isn't actually being sent.
            e.preventDefault();
            e.stopPropagation();
            if ($('#my-awesome-dropzone').valid() == true) {
                if (myDropzone.files.length) {
                    myDropzone.processQueue();
                    // upload files and submit the form
                } else {
                    $('#dropzonenoimageerrormessage').show();
                }
            }
        });

      }, // init end
        success: function(file, response){
            $.getJSON("<?php echo(base_url('upload/image')); ?>", function(data){
                window.location = data.urllink;
            });
        }

    };  

这是我的控制器中尝试发送 json 的片段

            if(move_uploaded_file($tempFile, $targetFile)){



                $newposturl = base_url('post/image/'.$data['recentlyuploadedimage']['slug']);

                $arr = array('urllink' => "$newposturl");

                echo json_encode($arr);


            }

文件确实被移动了,数据被存储在数据库中,但我的 javascript 似乎无法获取 json

【问题讨论】:

  • success: 函数应该在 response 变量中返回来自 CI 的 json。你不需要使用$.getJSONSee this answer for example.
  • 这是否意味着我应该删除 remove $.getJSON 并只使用 windows.location = response.urllink;
  • @ourmandave 哦,它成功了,谢谢伙计!我如何将您的评论作为答案?

标签: php json codeigniter dropzone.js


【解决方案1】:

@ourmandave 给出了正确的答案,我只需要更改这行代码

    success: function(file, response){
        $.getJSON("<?php echo(base_url('upload/image')); ?>", function(data){
            window.location = data.urllink;
        });
    }

去掉$.getJSON并使用response变量,因为success函数会自动获取json响应,所以它看起来像这样

    success: function(file, response){
            window.location = response.urllink;
    }

【讨论】:

    猜你喜欢
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2015-12-21
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多