axjaxuploadify.js的若干问题

在写xx客户扫描系统时,要做导入,用到了这个js,但是一直提示118行对象不支持此属性或方法等问题
网上找了很久,发现一篇不错的博客,提供了解决办法:
   

问题:
1:无法带参数提交,只能上传文件;(
没有自己验证
2:运行时报:jQuery.handleError is not a function 错误;
3:执行成功后,始终指向error方法处理,无法执行sucess方法;

解决方法:
1:无法带参数提交,只能上传文件;
原作者一定是把这个代码当作练习来写的,只完成了文件提交这个功能。需要对代码做些许修改即可。有两处修改:
第一处是将原createUploadForm: function(id, fileElementId) 方法添加一个data参数,并将data中的数据拼接进去即可。代码如下:

createUploadForm: function(id, fileElementId,data) {
    //create form
    var formId = 'jUploadForm' + id;
    var fileId = 'jUploadFile' + id;
    var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
    var oldElement = jQuery('#' + fileElementId);
    var newElement = jQuery(oldElement).clone();
    jQuery(oldElement).attr('id', fileId);
    jQuery(oldElement).before(newElement);
    jQuery(oldElement).appendTo(form);

    // 新增加参数的支持
    if (data) {
        for (var i in data) {
            $('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
        }
    }

    //set attributes
    jQuery(form).css('position', 'absolute');
    jQuery(form).css('top', '-1200px');
    jQuery(form).css('left', '-1200px');
    jQuery(form).appendTo('body');
    return form;
},

第二处 是调用createUploadForm方法地方,如下所示:
ajaxFileUpload:function(s){
  // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout        
  s=jQuery.extend({},jQuery.ajaxSettings,s);
  varid=newDate().getTime();

  // 修改为调用添加参数的方法
  // var form = jQuery.createUploadForm(id, s.fileElementId);
  varform=jQuery.createUploadForm(id,s.fileElementId,s.data);

  vario=jQuery.createUploadIframe(id,s.secureuri);
  ...

2:运行时报:jQuery.handleError is not a function 错误;
这个错误是由于ajaxfileupload.js 是在jquery1.4.2版本之前写的,Jquery之后的版本已经没有了handleError 方法,所以可以将1.4.2版本中的该方法复制到该js中。

// handleError 方法在jquery1.4.2之后移除了,此处重写改方法
handleError:function(s,xhr,status,e){
    // If a local callback was specified, fire it
    if(s.error){
        s.error.call(s.context||s,xhr,status,e);
    }
    // Fire the global callback
    if(s.global){
        (s.context?jQuery(s.context):jQuery.event).trigger("ajaxError",[xhr,s,e]);
    }
},

3:执行成功后,始终指向error方法处理,无法执行sucess方法;
这个是由于ajaxfileupload.js 处理返回data的时候,没有考虑后台返回的是字符串的问题(即使返回的JSON格式数据,我们也大多喜欢转化为字符串来返回)。修改uploadHttpData方法:
uploadHttpData: function( r, type ) {
        var data = !type;
        data = type == "xml" || data ? r.responseXML : r.responseText;
        // ifthe type is "script", eval it in global context
        if( type == "script" )
        {
         jQuery.globalEval( data );
        }

        // Get the JavaScript object, ifJSON is used.
        if( type == "json" )
        {
         eval( "data = " + data );
        }

        // evaluate scripts within html
        if( type == "html" )
        {
         jQuery("<div>").html(data).evalScripts();
        }

        return data;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值