2024年4月26日发(作者:)
js 上传报错的解决方法_
这篇文章主要为大家具体介绍了js 上传报错的解决方法,感爱好的小
伙伴们可以参考一下
信任大家在工作中常常用到文件上传的操作,由于我是搞前端的,所以这里主要是介
绍ajax在前端中的操作。代码我省略的比较多,挺直拿js那里的
$.ajaxFileUpload({
url:'.coding/mobi/file/',//处理图片脚本
secureuri :false,
fileElementId :'image2',//file控件id。就是input type="file" id="image2"
dataType : 'json',
success : function (data, status){
(data);
},
error: function(data, status, e){
alert(e);
}
})
根据教程,这样子上传的话是没有问题的,可是它始终有一个报错。报的是什么错有
点忘了,不好意思 ,由于用完很久才记得补回这篇文章,但是要修改它的源码,那个错误
就可以解决了
它源码的最终一段是这样子的
uploadHttpData: function( r, type ) {
var data = !type;
data = type == "xml" || data ? seXML : seText;
// If the type is "script", eval it in global context
if ( type == "script" )
Eval( 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 ? seXML : seText;
// If the type is "script", eval it in global context
if ( type == "script" )
Eval( data );
// Get the JavaScript object, if JSON is used.
if ( type == "json" ){
// 由于json数据会被pre标签包着,所以有问题,现在添加以下代码,
// update by hzy
var reg = /pre.+?(.+)pre/g;
var result = (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 报这错Error is not a function
版本1.4.2之前的版本才有handlerError方法,例子里用法的Jquery是1.2的,解
决方法:
为了能够连续用法ajaxfileupload上传我们的附件,只好将下面代码拷进我们的项目
中的文件中
handleError: function( s, xhr, status, e ) {
// If a local callback was specified, fire it
发布评论