最近做一个文件上传的功能,还要绑定数据传输到后台,为了不影响前端的体验,采用ajax发送请求。找了一些资料,网上的用ajaxupload这个插件。
但是无论成功还是失败都是执行的error的回调函数。后台我采用springmvc返回的json,在前段debug后发现,原来返回的虽然是json格式,但是它又包装了一层。
{"retcode":"0"}
没办法查了好多资料发现有的大牛也遇到了这个问题,有的还琢磨了好几天。看了一下github上的源码是6年前上传的,发现有一个大牛2016.11.15号对源码修改了下解决了这个问题。uploadHttpData: function( r, type ) {
var data = !type;
data = type == "xml" || data ? r.responseXML : r.responseText;
// If the type is "script", eval it in global context
if ( type == "script" )
jQuery.globalEval( data );
// Get the JavaScript object, if JSON is used.
if ( type == "json" )
{
// If you add mimetype in your response,
// you have to delete the '
' tag.// The pre tag in Chrome has attribute, so have to use regex to remove
var data = r.responseText;
var rx = new RegExp("
(.*?)","i");var am = rx.exec(data);
//this is the desired data extracted
var data = (am) ? am[1] : ""; //the only submatch or empty
eval( "data = " + data );
}// evaluate scripts within html if ( type == "html" ) jQuery("
估计原作者对代码估计也没维护了。我最后还找到了大牛们的另一个解决办法:在ajaxFileUpload.js中的uploadHttpData方法中修改对json的判断:
这里只是记录下防止自己忘记哈