Steven
Steven

Reputation: 19425

Not able to get JSON data from Plupload

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?

Upvotes: 4

Views: 7734

Answers (2)

danh pham
danh pham

Reputation: 29

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);
});

Upvotes: 3

nanobar
nanobar

Reputation: 66355

There are a number of things wrong

  1. Remove the quote escaping \"
  2. response + status should be in quotes

i.e.

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

Upvotes: 2

Related Questions