码迷,mamicode.com
首页 > Web开发 > 详细

ajax上传文件以及使用中常见问题

时间:2016-05-09 20:10:37      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

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

<script type="text/javascript">
$(function () {
$(":button").click(function () {
if ($("#file1").val().length > 0) {
ajaxFileUpload();
}
else {
alert("请选择图片");
}
})
})
function ajaxFileUpload() {
$.ajaxFileUpload
(
{
url: ‘/api/poi/picture-upload‘, //用于文件上传的服务器端请求地址
type: ‘post‘,
secureuri: false, //一般设置为false
fileElementId: ‘file1‘, //文件上传空间的id属性 <input type="file" id="file" name="file" />
dataType: ‘json‘, //返回值类型 一般设置为json
success: function (data, status) //服务器成功响应处理函数
{
console.log(data);
$("#img1").attr("src", data.imgPath1);
if (typeof (data.error) != ‘undefined‘) {
if (data.error != ‘‘) {
alert(data.error);
} else {
alert(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] );
}
}

ajax上传文件以及使用中常见问题

标签:

原文地址:http://www.cnblogs.com/gq365/p/5475043.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!