dropzone.js重定向后成功不工作从PHP codeigniter返回的json

我试图从我的控制器回声json_encode使用$ .getJSON上传我的dropzone成功后重新导向,但它似乎是我的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

author: boj-slofar
采纳答案:

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

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

删除$ .getJSON并使用响应变量,因为成功函数自动获取json响应,所以它看起来像这样

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

参考更多解答: dropzone.js redirect after success not working from json returned by php codeigniter ,转载请保留出处dropzone.js重定向后成功不工作从PHP codeigniter返回的json及作者信息

Statement: We respect knowledge and authors. Since the content comes from the Internet and is intended for scientific research, any reprinters should retain the author's signature and origin. If you are the author of the content and feel in dispute, please contact email: 1076545519@qq.com. We will find out the situation and deal with it in time. We sincerely thank the author for his hard work.


更多:dropzone.js