Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fetch response text from ajax response of a dropzone

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

like image 272
Ashik Basheer Avatar asked Apr 29 '14 20:04

Ashik Basheer


2 Answers

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.

like image 149
rphv Avatar answered Oct 18 '22 15:10

rphv


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);});
like image 22
Asad Zaheer Avatar answered Oct 18 '22 13:10

Asad Zaheer