ajax上傳文件以及使用中常見問題處理


<script src="/scripts/ajaxfileupload.js"></script>
<script src="/scripts/jquery2.1.1.min.js"></script>

<script type="text/javascript">
   $(function () { $(":button").click(function () { if ($("#fileLoad").val().length > 0) { ajaxFileUpload(); } else { alert("請選擇文件!"); } }) }) function ajaxFileUpload() { $.ajaxFileUpload ( { url: '/api/poi/picture-upload', //用於文件上傳的服務器端請求地址 type: 'post', secureuri: false, //一般設置為false fileElementId: 'fileLoad', //文件上傳空間的id屬性 <input type="file" id="fileLoad" name="file" /> dataType: 'json', //返回值類型 success: function (data, status) //服務器成功響應處理函數  { $("#img1").attr("src", data.data); if (typeof (data.error) != 'undefined') { if (data.error != '') { console.log(data.error); } else { console.log(data.msg); } } }, error: function (data, status, e)//服務器響應失敗處理函數  { console.log(data); } } ) return false; } </script>

 

ajaxFileUpload 上傳成功后卻執行 error:返回的json數據里多了一些信息導致

ajaxfileupload.js中加這塊:
if ( type == "json" )
    var start = data.indexOf('{'); var end = data.indexOf('}')+1; data = data.substring(start,end); eval( "data = " + data );

ajaxFileUpload 報這錯jQuery.handleError is not a function:版本問題。

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM