js ajaxfileupload.js上傳報錯的解決方法
來源:易賢網(wǎng) 閱讀:1504 次 日期:2016-07-06 09:22:16
溫馨提示:易賢網(wǎng)小編為您整理了“js ajaxfileupload.js上傳報錯的解決方法”,方便廣大網(wǎng)友查閱!

這篇文章主要為大家詳細介紹了js ajaxupload.js上傳報錯的解決方法,感興趣的小伙伴們可以參考一下

相信大家在工作中經(jīng)常用到文件上傳的操作,因為我是搞前端的,所以這里主要是介紹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 ? 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數(shù)據(jù)會被<pre>標簽包著,所以有問題,現(xiàn)在添加以下代碼,

   // update by hzy

   var reg = /<pre.+?>(.+)<\/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

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

為了能夠繼續(xù)使用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] ); 

        } 

  }

以上就是面對ajaxupload.js上傳報錯問題的解決方法,希望能幫助大家解決困難

更多信息請查看網(wǎng)絡編程
易賢網(wǎng)手機網(wǎng)站地址:js ajaxfileupload.js上傳報錯的解決方法

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)