今天练习ssm框架的时候发现的问题:
controller里的代码:
@RequestMapping(value = "check", method = RequestMethod.GET)
@ResponseBody
public Object userCheck(@RequestParam String name) throws Exception {
List<Bookonline> bookonline = bookOnlineService.getBookOnline(name);
Map<String, String> map = new HashMap<>();
if (bookonline != null) {
map.put("info", name); //这里传入的json
} else {
map.put("info", "noexist");
}
return JSONArray.toJSONString(map);
}
ajax里的代码:
name.bind("blur", fuction() {
var oldname = document.getElementById("oldname");
$.ajax({
type: "get",
url: "check",
data: {name: name.val()},
dataType: "json",
success: function (data) { //data:返回数据(json对象)
alert( name.val() === oldname.val()); //这里是要比较的值
},
error: function (data) {
alert ("error");
}
})
})
如上代码,在要比较的值明明相等的情况下,alert打印出来的却是 false
怎么解决这个问题呢?
解:
需要在你的springmvc里配置消息装换器,加上如下代码:
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
<value>application/xml;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
网友评论