从dropzone的ajax响应中获取响应文本

Ash*_*eer 4 javascript ajax dropzone.js

我从dropzone成功上传得到了以下回复.

从这里我需要获取responseText

我尝试过:console.log(response.xhr.responseText)这会显示完整的回复文字.

当我尝试img从这样的responseText中获取console.log(response.xhr.responseText.img)控制台时抛出undefined

rph*_*phv 7

在您的示例中,值rexponse.xhr.responseText是字符串,而不是对象.您可以将字符串解析为对象并访问该img属性:

(JSON.parse(response.xhr.responseText)).img

...但是,由于Dropzone success事件同时获得file对象和解析responseText,您可以编写如下success处理程序:

new Dropzone("#myUploader", { 
    url: "/upload",
    init: function () {
      this.on("success", function (file, responseText) {
        console.log(responseText.img);
      });
    }
});
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请查看常见问题解答.