今天遇到一个问题, 折腾了一个下午。
就是当我在spring-servlet,xml配置FastJson的消息转化器时(代码如下),在js文件中运用Ajax的异步判断就会失效(代码如下)。
在spring-servlet,xml配置FastJson的消息转化器
<!--配置FastJson的消息转化器 —— FastJsonHttpMessageConverter -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="features">
<list>
<!--输出Date的日期转化器-->
<value>WriteDateUseDateFormat</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
我想说的Ajax的异步判断:
userCode.bind("blur",function(){
//ajax后台验证--userCode是否已存在
$.ajax({
type:"GET",//请求类型
url:path+"/user/ucexist.html",//请求的url
data:{userCode:userCode.val()},//请求参数
dataType:"json",//ajax接口(请求url)返回的数据类型
success:function(data){//data:返回数据(json对象)
if(data.userCode == "exist"){//账号已存在,错误提示
validateTip(userCode.next(),{"color":"red"},imgNo+ " 该用户账号已存在",false);
}else{//账号可用,正确提示
validateTip(userCode.next(),{"color":"green"},imgYes+" 该账号可以使用",true);
}
},
error:function(data){//当访问时候,404,500 等非200的错误状态码
validateTip(userCode.next(),{"color":"red"},imgNo+" 您访问的页面不存在",false);
}
});
}).bind("focus",function(){
//显示友情提示
validateTip(userCode.next(),{"color":"#666666"},"* 用户编码是您登录系统的账号",false);
}).focus();
怎么解决呢?
唉,把配置的FastJson的消息转化器的代码删掉就行了
但是为什么配置的FastJson的消息转化器会与js文件中运用Ajax的异步判断发生冲突呢?这得等你们大神回复
若你想利用FastJson进行日期转化又不与Ajax的异步判断发生冲突的话,也可以不用配置的FastJson的消息转化器,具体参见FastJson的日期转化格式之多种配置方式,有其它两种方法
晚上我在想了一会儿,发现问题所在
另附上我在Controller层的处理方法
@RequestMapping(value="/ucexist.json")
@ResponseBody
public Object userCodeIsExit(@RequestParam String userCode){
logger.debug("userCodeIsExit userCode===================== "+userCode);
HashMap<String, String> resultMap = new HashMap<String, String>();
if(StringUtils.isEmpty(userCode)){
resultMap.put("userCode", "exist");
}else{
User user = userService.selectUserCodeExist(userCode);
if(null != user)
resultMap.put("userCode", "exist");
else
resultMap.put("userCode", "noexist");
}
return JSONArray.toJSONString(resultMap);
}
我把它转化为对象转化成JSON字符串,没有直接把获取的对象返回,简单来说并没有用到FastJson的消息转化器 —— FastJsonHttpMessageConverter的精髓。
这就是产生冲突的所在吧,这句看不懂?参考我的另一个博客配置FastJson的消息转化器 —— FastJsonHttpMessageConverter注意事项