ajaxfileupload.js回调异常

来源:互联网 发布:数码网络侦探 攻略 编辑:程序博客网 时间:2024/06/13 14:29

上传文件时采用了ajaxfileupload.js来做处理,但是数据上传成功之后原本写好的回调函数并未被调用,经过分析与查证,确认是ajaxfileupload.js内部方法问题,现将解决方案贴出如下:

$.ajaxFileUpload(            {                url:"../empty,                secureuri:false,                fileElementId:"file_ele_id",                dataType: "text",                success: function (data,status){                    var rx = new RegExp("<pre.*?>(.*?)</pre>","i");                    var am = rx.exec(data);                    data = (am) ? am[1] : "";                    eval("data = " + data );                    alert(data.msg);                    if(status){                        CloseWindow("ok");                    }                },                error: function (jqXHR, textStatus, errorThrown)                {                    alert(errorThrown);                    CloseWindow();                }            });}
原回调异常方案如下:

$.ajaxFileUpload(            {                url:"../em,                secureuri:false,                fileElementId:"file_ele_id",                dataType: "json",                success: function (data,status){                    alert(data.msg);                    if(status){                        CloseWindow("ok");                    }                },                error: function (jqXHR, textStatus, errorThrown)                {                    alert(textStatus);                    CloseWindow();                }            });}
异常原因分析为ajaxfileupload.js内以下代码对type为json时对data的不当处理引起,因为后台传至界面的json数据已经被<pre style=">json<pre/>给围起来了,eval()函数在处理data时抛出了异常并在ajaxfileupload.js内部进行了try{}catch(){}为error,所以即便后台返回的是处理成功的json,回调函数依然走的是error

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" )  {       eval("data = " + data );       // eval('(' + data + ')');//eval("\""+data+"\"");    }    // evaluate scripts within html      if ( type == "html" )          jQuery("<div>").html(data).evalScripts();          //alert($('param', data).each(function(){alert($(this).attr('value'));}));    return data;} 


原创粉丝点击