首页 > web前端 > js教程 > 正文

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

php中文网
发布: 2016-05-16 15:02:04
原创
2120人浏览过

相信大家在工作中经常用到文件上传的操作,因为我是搞前端的,所以这里主要是介绍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("<div>").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 &#63; 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数据会被<pre class="brush:php;toolbar:false;">标签包着,所以有问题,现在添加以下代码,
   // update by hzy
   var reg = /<pre class="brush:php;toolbar:false;".+&#63;>(.+)<\/pre>/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("<div>").html(data).evalScripts();
   //alert($('param', data).each(function(){alert($(this).attr('value'));}));
  return data;
 }
登录后复制

这样就可以正常使用了。

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

JoyPix AI
JoyPix AI

轻松制作AI视频、AI数字人,支持文生视频、声音克隆

JoyPix AI 243
查看详情 JoyPix AI

立即学习Java免费学习笔记(深入)”;

版本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 &#63; jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] ); 
        } 
  } 
登录后复制

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

相关标签:
js
java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号