Not able to get JSON data from Plupload

只愿长相守 提交于 2019-12-10 15:52:54

问题


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?


回答1:


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
}



回答2:


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


来源:https://stackoverflow.com/questions/10313319/not-able-to-get-json-data-from-plupload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!