WEB前端 和 JAVA后台 关于头像上传功能实现中出现的ajax 404问题经验:
1> 检查框架,框架的兼容性,对于post、get的支持度以及使用方式。
本次头像上传功能使用的是ajaxfileupload.js, 其中遇到的问题是运行报错
$.handleError is not a function
问题原因:由于ajaxfileupload.js 是在jquery1.4.2版本之前写的,jQuery之后的版本已经没有了handleError 方法
解决方案: 手动添加jQuery脚本,代码如下
// handleError 方法在jquery1.4.2之后移除了,此处重写改方法
$.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] );
}
}
此插件其他问题: 传送门http://blog.yadgen.com/?p=970
2> 检查POST请求内容,
主要看请求头信息的HOST地址,以及Accept-Language(排除地址错误和编码错误)
3> 检查POST的data
$ajax({
url:php的url,
data:{
这里可以描述为你对数据的一些操作
},
success:function(str){
alert(typeof(str));
}
});
也就是上述代码中str类型:
正确JSON,object类型
如果是string类型,需要进行转化:
转化条件:
字符串中,尤其是路径不允许有反斜扛”\”,如果有也要2个反斜杠”\”
转换方式:
var jsonString = '{"localPath":"D:/Program Files/apache-tomcat-7.0.57/webapps/spring_mybatis/upload\\temp\\QQ图片20151117162607.png","picSize":"0.1351884765625"}';
var jsonReplace = jsonString.replace(/\\/g, '\/');
var jsonOut = eval('('+ jsonReplace +')');
4> 检查后台框架对于的data类型的支持方式
有些后台不支持JSON的数据格式。