controller层传json数据到ajax的问题:和页面获取的值比较,明明值一样却不相等的原因

今天练习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>

问题解决!!!

copyright © Tmiracle all right reserved,powered by Gitbook该文件最后修改时间: 2019-08-30 16:14:32

results matching ""

    No results matching ""