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
- 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
3 Answers
Reset to default 9In 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); }); } });