栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

js ajaxfileupload.js上传报错的解决方法

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

js ajaxfileupload.js上传报错的解决方法

相信大家在工作中经常用到文件上传的操作,因为我是搞前端的,所以这里主要是介绍ajax在前端中的操作。代码我省略的比较多,直接拿js那里的

$.ajaxFileUpload({
   url:'www.coding/mobi/file/uploadSingleFile.html',//处理图片脚本
   secureuri :false,
   fileElementId :'image2',//file控件id。就是input type="file" id="image2"
   dataType : 'json',
   success : function (data, status){
    console.log(data);
   },
   error: function(data, status, e){
    alert(e);
   }
   })

按照教程,这样子上传的话是没有问题的,可是它一直有一个报错。报的是什么错有点忘了,不好意思 ,因为用完很久才记得补回这篇文章,但是要修改它的源码,那个错误就可以解决了 

它源码的最后一段是这样子的

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

将这一段改为这样子

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" ){
  // 因为json数据会被
标签包着,所以有问题,现在添加以下代码,
  // update by hzy
  var reg = /(.+)
/g; var result = data.match(reg); result = RegExp.$1; // update end data = $.parseJSON(result); // eval( "data = " + data ); // evaluate scripts within html } if ( type == "html" ) jQuery("").html(data).evalscripts(); //alert($('param', data).each(function(){alert($(this).attr('value'));})); return data; }

这样就可以正常使用了。

另一种情况:ajaxFileUpload 报这错jQuery.handleError is not a function

版本1.4.2之前的版本才有handlerError方法,例子里使用的Jquery是1.2的,解决方法:

为了能够继续使用ajaxfileupload上传我们的附件,只好将下面代码拷进我们的项目中的ajaxfileupload.js文件中

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] ); 
    } 
 } 

更多精彩内容请参考专题《ajax上传技术汇总》,《javascript文件上传操作汇总》和《jQuery上传操作汇总》进行学习。

以上就是面对ajaxupload.js上传报错问题的解决方法,希望能帮助大家解决困难,也希望大家继续关注考高分网更多精彩内容。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/97293.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号