问题
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
- Remove the quote escaping \"
- 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