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] );
    }
}
原文地址:https://www.cnblogs.com/gq365/p/5508464.html