0

I'm using Blueimp's JQuery File Upload plugin, and so upon successful upload the callback function is:

$('#fileupload').fileupload('send', {files: filesList})
    .success(function (result, textStatus, jqXHR) { console.log('success'); })
    .error(function (jqXHR, textStatus, errorThrown) { console.log('error'); })
    .complete(function (result, textStatus, jqXHR) { 
        console.log('complete: ' + JSON.stringify(result));
        console.log(result.responseText.files[0].name);
});

So all I'm looking at is the .complete function. The first console.log returns:

complete: {"readyState":4,"responseText":"{"files":[{"name":"video.mp4","type":"video/mp4","size":2348842}]}","responseJSON":{"files":[{"name":"video.mp4","type":"video/mp4","size":2348842}]},"status":201,"statusText":"Created"}

complete is technically result, and under that is responseText and under that is files, which is an array, and name is a key/property in that array.

So when I try to console.log result.responseText.files[0].name, it says Uncaught TypeError: Cannot read property '0' of undefined.

Could someone please find what's wrong? Thanks!

1 Answer 1

3

result.responseText is a string

you need to convert to an object: JSON.parse(result.responseText)

response = JSON.parse(result.responseText);
console.log(response.files[0].name);

will work

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! I will accept and upvote your answer in 11 minutes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.