js ajaxfileupload.js上傳報(bào)錯(cuò)的解決方法
相信大家在工作中經(jīng)常用到文件上傳的操作,因?yàn)槲沂歉闱岸说模赃@里主要是介紹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); } })
按照教程,這樣子上傳的話是沒有問(wèn)題的,可是它一直有一個(gè)報(bào)錯(cuò)。報(bào)的是什么錯(cuò)有點(diǎn)忘了,不好意思 ,因?yàn)橛猛旰芫貌庞浀醚a(bǔ)回這篇文章,但是要修改它的源碼,那個(gè)錯(cuò)誤就可以解決了
它源碼的最后一段是這樣子的
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" ){ // 因?yàn)閖son數(shù)據(jù)會(huì)被<pre>標(biāo)簽包著,所以有問(wèn)題,現(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 報(bào)這錯(cuò)jQuery.handleError is not a function
版本1.4.2之前的版本才有handlerError方法,例子里使用的Jquery是1.2的,解決方法:
為了能夠繼續(xù)使用ajaxfileupload上傳我們的附件,只好將下面代碼拷進(jìn)我們的項(xiàng)目中的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] ); } }
更多精彩內(nèi)容請(qǐng)參考專題《ajax上傳技術(shù)匯總》,《javascript文件上傳操作匯總》和《jQuery上傳操作匯總》進(jìn)行學(xué)習(xí)。
以上就是面對(duì)ajaxupload.js上傳報(bào)錯(cuò)問(wèn)題的解決方法,希望能幫助大家解決困難,也希望大家繼續(xù)關(guān)注腳本之家更多精彩內(nèi)容。
- 詳解ajax的data參數(shù)錯(cuò)誤導(dǎo)致頁(yè)面崩潰
- 快速解決ajax請(qǐng)求出錯(cuò)狀態(tài)碼為0的問(wèn)題
- PHP 中使用ajax時(shí)一些常見錯(cuò)誤總結(jié)整理
- 完美解決ajax跨域請(qǐng)求下parsererror的錯(cuò)誤
- ajax跨域訪問(wèn)報(bào)錯(cuò)501的解決方法
- jQuery中ajax錯(cuò)誤調(diào)試分析
- Ajax向后臺(tái)傳json格式的數(shù)據(jù)出現(xiàn)415錯(cuò)誤的原因分析及解決方法
- Ajax犯的錯(cuò)誤處理方法
- 解決ajax返回驗(yàn)證的時(shí)候總是彈出error錯(cuò)誤的方法
- Jquery Ajax Error 調(diào)試錯(cuò)誤的技巧
- django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤如何解決
- Ajax報(bào)錯(cuò)400的參考解決辦法
相關(guān)文章
JavaScript輸出所選擇起始與結(jié)束日期的方法
這篇文章主要介紹了JavaScript輸出所選擇起始與結(jié)束日期的方法,涉及javascript結(jié)合HTML5元素操作日期運(yùn)算的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07詳解springmvc 接收json對(duì)象的兩種方式
本篇文章主要介紹了springmvc 接收json對(duì)象的兩種方式,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12

js 實(shí)現(xiàn)watch監(jiān)聽數(shù)據(jù)變化的代碼

如何在js代碼中消滅for循環(huán)實(shí)例詳解