Not able to get JSON data from Plupload

Steven picture Steven · Apr 25, 2012 · Viewed 7.6k times · Source

I'm uploading files using Plupload and it works fine.

I've tested various suggestions found here on Stackoverflow, but I'm still not able to get any sensible data from my JSON respons.

In my upload.php file I have echo json_encode($result);

In my JS I do the following:

  uploader.bind('FileUploaded', function(up, file, response) {
    var obj = jQuery.parseJSON(response);
    var obj2 = eval(response);

    alert(response.toSource()); // <-- Outputs raw data
    alert(obj); // <-- is NULL
    alert(obj2.toSource()); // <-- Outputs eval data format
    alert(obj2.logo_url); // <-- Is not working
  });

The alert(response.toSource()); returns this:

({response:"{
  \"logo_url\":\"http:\\/\\/mysite.com\\/uploads\\/3b\\/7b019482c806f9_logo.jpeg\",
  \"img_id\":\"30\",
  \"feedback\":{\"message\":\"File uploaded\",
  \"success\":true}}", 
  status:200})

and obj is NULL.

What am I doing wrong here?

Answer

danh pham picture danh pham · Nov 15, 2013

var uploader = $("#multi_upload").pluploadQueue();

uploader.bind('FileUploaded', function (up, file, res) {
    var res1 = res.response.replace('"{', '{').replace('}"', '}');
    var objResponse = JSON.parse(res1);
    alert(objResponse.fn);
});