如何访问Json Response [英] How to access Json Response
问题描述
以下是我将文件上传到服务器的代码。
Here is my code for uploading the files to my server.
$(function () {
$('#phy_fileupload').fileupload({
autoUpload: true,
add: function (e, data) {
$('body').append('<p class="upl">Uploading...</p>')
data.submit();
},
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$('#phy_progress .bar').css(
'width',
progress + '%'
);
},
done: function (e, data) {
$.each(data.files, function (index, file) {
$('.upl').remove();
$('<p/>').text(file.name).appendTo("#phy_files");
});
}
});
});
这是HTML
<div id="phy_progress">
<div class="bar" style="width: 0%;"></div>
</div>
<input id="phy_fileupload" type="file" name="files[]" data-url="<?php echo site_url('admin/upload/'); ?>" multiple>
<div class="files" id="phy_files"></div>
效果非常好,上传图片并发送json作为回复,我可以在FIREBUG中看到。
It works very well, uploads the picture and send json in response which i can see in FIREBUG.
以下是一个上传文件的回复。
Here is Response of one uploaded file.
{"files":[{"name":"2013-06-20_1235UQn5gm5SRUPzbA.png","size":29381,"url":"http:\/\/files\/2013-06-20_1235UQn5gm5SRUPzbA.png","thumbnail_url":"http:\/\/files\/thumbnail\/2013-06-20_1235UQn5gm5SRUPzbA.png","delete_url":"http:\/\/localhost\/mark\/?file=2013-06-20_1235UQn5gm5SRUPzbA.png","delete_type":"DELETE"}]}
现在问题是它在div中添加了文件名(#phy_files)(即2013-06-20_1235.png),我怎么想添加上传到服务器的新文件名(即)2013 -06-20_1235UQn5gm5SRUPzbA.png。现在我花了一整天的时间来尝试访问Json的这个响应,但显然要么我一直在寻找错误的结局,要么我完全迷失了,我到目前为止还不知道如何才能访问这个响应。
Now the issue is that it adds the name of file in the div (#phy_files) (i.e 2013-06-20_1235.png), how ever I want to add the new file name which was uploaded to the server (i.e) 2013-06-20_1235UQn5gm5SRUPzbA.png. Now I spent all day to try to access this response of Json, but apparently either i have been looking at the wrong end or I'm completely lost, I have so far no idea how can i access this response.
任何帮助都会非常感激。如果您不理解我在这里写的内容,请告诉我,如果需要,我很乐意再次解释。谢谢。
Any help would really be appreciated. Please let me know if you don't understand what i have written here, I'm more than happy to explain it again if required. Thank you.
简而言之:
问题是上传的文件名为let say' file.png',在服务器上将其名称更改为'filexyzserdsdf.png',现在我希望将响应文件名写入my div(#phy_files),但它正在写'file.png'而不是
what the issue is that the file being uploaded is named let say 'file.png', on server its name is changed to 'filexyzserdsdf.png', now i want the response file name to be written in the my div (#phy_files) , but it is writing 'file.png ' instead
推荐答案
现在它变得清晰......好吧,你应该这样做:
now its getting clear... ok you should do this then :
done: function (e, data) {
var res = jQuery.parseJSON(data.result);
$.each(res.files, function (index, file) {
$('.upl').remove();
$('<p/>').text(file.name).appendTo("#phy_files");
});
}
这篇关于如何访问Json Response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!