最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fetch response text from ajax response of a dropzone - Stack Overflow

programmeradmin5浏览0评论

I've got the following response from dropzone successful upload.

From this I need to fetch the responseText

I tried: console.log(response.xhr.responseText) this show the full response text.

When i try to fetch img from responseText like this console.log(response.xhr.responseText.img) the console throw undefined

I've got the following response from dropzone successful upload.

From this I need to fetch the responseText

I tried: console.log(response.xhr.responseText) this show the full response text.

When i try to fetch img from responseText like this console.log(response.xhr.responseText.img) the console throw undefined

Share Improve this question edited Feb 8, 2017 at 14:52 CommunityBot 11 silver badge asked Apr 29, 2014 at 20:04 Ashik BasheerAshik Basheer 1,6014 gold badges17 silver badges36 bronze badges 2
  • It looks like you might be using a JavaScript library (something like BlueImp's fileUpload), but I can't tell. It might help to clarify whether you're using a library to handle this, and what that library is. – Sean Quinn Commented Apr 29, 2014 at 20:24
  • I'm using dropzone.js – Ashik Basheer Commented Apr 30, 2014 at 6:14
Add a ment  | 

3 Answers 3

Reset to default 9

In your sample, the value of rexponse.xhr.responseText is a string, not an object. You can parse the string into an object and access the img property:

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

...but, since Dropzone success events get both the file object and the parsed responseText, you could write a success handler like this:

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

for more information, check out the FAQ.

Saurabh solution is working for me after making some changes, beacuse i was getting

Uncaught SyntaxError: Unexpected token o

when trying

init: function () { 
this.on("success", function (file, responseText) { 
var responsetext = JSON.parse(responseText); 
console.log(responsetext.file_name); });

so i made some changes in it and its working for me.

this.on("success", function (file) {
                var responsetext = JSON.parse(file.xhr.responseText);
                console.log(responsetext);});

new Dropzone("#myUploader", { url: "/upload", init: function () { this.on("success", function (file, responseText) { var responsetext = JSON.parse(responseText); console.log(responsetext.file_name); }); } });

发布评论

评论列表(0)

  1. 暂无评论