这个问题迷惑了我好几天,都快要放弃了,功夫不负有心人,最终成功解决,下面写一下我的解决方法。

我传的数据是json类型的,执行失败的回调函数是因为从后台传过来的数据不是严格的json类型,所以才会不执行成功的回调函数。

下面贴一下我的代码

Controller

 @RequestMapping(value="/reg")
@ResponseBody
public Map<String,Object> Register(User user) throws IOException{
Map<String,Object> map = new HashMap<String,Object>();
boolean isSuccess = userService.Register(user);
if(isSuccess){
map.put("tip", "success");
}
else{
map.put("tip", "error");
}
return map;
}

jsp


$.ajax({
type:'POST',
data: {"email":email,"password":password,"type":type},
url:'user/reg',
async:false,
dataType: 'json',
success:function(response){
alert(response.tip);
$(form).find(":submit").attr("disabled", false);
},
error:function(response){
alert(response.tip);
$(form).find(":submit").attr("disabled", false);
}
});

注意我使用了注解@ResponseBody,该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

要想使用@ResponseBody需要在配置文件中启用注解,下面是启用注解的代码

  <!--开启注解-->
<mvc:annotation-driven />

SpringMVC中使用@ResponseBody注解标注业务方法,将业务方法的返回值做成json输出给页面

除了一些spring的包之外,还需要jackson-annotations.jar , jackson-core.jar , jackson-databind.jar 这三个包

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.4</version>
</dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.4</version>
</dependency>

随机推荐

  1. 工作总结 mvc外键 public virtual SysUser TransferUser { get; set; } 必须要加 virtual 否则 TransferUser 值为null 还要加[ForeignKey("TransferUser")] Bind 和 ScaffoldColumn(转)

    [Table("T_SYS_TRANSFERUSER")] public class SysTransferUser : DbSetBase { [ForeignKey(" ...

  2. 自己定义struts2中action类型转换器

    DateAction.java中代码例如以下: package com.itheima.action; import java.util.Date; public class DateAction { ...

  3. Oracle pipe

    初次接触到Report ,看到了它背后复杂的SQL操作, 首先看到了一个Pipe,先了解下PIPE的用法: 关键字PIPELINED表明这是一个oracle管道函数,oracle管道函数的返回值类型必 ...

  4. Django-form组件补充

    首先来看一个用户登录的实例 from django.forms import Form from django.forms import fields from django.forms import ...

  5. Hadoop文档 索引

    Hadoop中文文档 http://hadoop.apache.org/docs/r1.0.4/cn/index.html Hadoop资料整理 http://www.itpub.net/thread ...

  6. 什么是SDN(软件定义网络)(转载)

    软件定义网络(Software Defined Network, SDN)在InfoWorld于2011年11月公布的将影响未来10年的十项新技术中排名第二.2012年7月,SDN代表厂商Nicira ...

  7. 在html中显示Flash的代码

    <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://down ...

  8. stretchableImageWithLeftCapWidth

    本文转载至 http://www.cnblogs.com/bandy/archive/2012/04/25/2469369.html (NSInteger)topCapHeight 这个函数是UIIm ...

  9. registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later

    本文转载至 http://bbs.csdn.net/topics/390889517 IOS8 PUSH解决方法 昨天晚上整理PUSH的东西,准备些一个教程,全部弄好之后,发现没有达到预期的效果,本以 ...

  10. mongodb学习之:文档操作

    在上一章中有讲到文档的插入操作是用insert的方法.如果该集合不在该数据库中,mongodb会自动创建该集合并插入文档 用find的方法可以查找所有的集合数据 > db.maple.find( ...