我正在使用这个脚本 http://hayageek.com/docs/jquery-upload-file.php
我的上传者。
上传很棒,一切正常,我的服务器端脚本在成功上传后返回JSON,但是,在成功事件中,我无法获取json值
$("#fileuploader").uploadFile({
url: site.url + 'main/upload',
fileName: 'cpp',
multiple: false,
method: "POST",
allowedTypes: "png,gif,jpg,jpeg",
dragDrop:true,
onSuccess:function(files,data,xhr)
{
if(data.error){
//there is an error
} else {
//there is no error
console.log('no errors ' + data.path);
$('.img-thumbnail').attr('src', data.path);
}
},
onError: function(files,status,errMsg)
{
console.log('uplaod failed');
}
});
我的服务器端脚本返回的JSON是 (如萤火虫所见)
{"error":false,"msg":"Success","path":"http:\/\/www.domain.com\/uploads\/thumb.png"}
但是,data.path返回undefined(我使用console.log来检查),但是当我用alert(数据)输出时,数据似乎完好无损。 (p / s:data.error,data.msg也返回未定义的值)。
这是我的逻辑错了还是我错过了什么?
答案 0 :(得分:0)
使用 returnType:“json”和
尝试使用 parseJSON() ,
onSuccess:function(files,data,xhr)
{
data=$.parseJSON(data); // yse parseJSON here
if(data.error){
//there is an error
} else {
//there is no error
console.log('no errors ' + data.path);
$('.img-thumbnail').attr('src', data.path);
}
},