今天練習ssm框架的時候發(fā)現(xiàn)的問題:
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:返回數(shù)據(jù)(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>