【springMVC 后台跳转前台】1.使用ajax访问的后台,后台正常执行,返回数据,可是不能进入前台的ajax回调函数中 ----2.先后台都没有报错,不能进入ajax回调函数

问题1:html

使用ajax访问的后台,后台正常执行,而且正常返回数据,可是不能进入前台的ajax回调函数中ajax

问题展现:spring

 

 问题解决:json

最后发现是由于后台的方法并未加注解:@ResponseBody,致使方法不认识最后返回的是给ajax的data,而是觉得要去找这个页面因此并未找到!!app

1 @RequestMapping("/queryAllDisease")
2     @ResponseBody
3     public PageInfo<Disease>  queryAllDisease(String productId, ModelMap model, int pageNo , int pageSize){
4             Product product =new Product();
5             product.setProductId(productId);
6             Criteria criteria = getCurrentSession().createCriteria(Disease.class);
7             criteria.add(Restrictions.eq("product", product));
8             return diseaseService.findQuery(criteria, pageNo, pageSize);
9     }
View Code

 

 

 

一样的,若是Controller中的方法执行完成以后  不想返回前台,就此打住,则也须要加上@ResponseBodyjsp

由于即便方法返回值为voidide

spring也会按照前台请求过来的页面地址去找,找不到就会以下:函数

 

因此,在后台:【如下的代码依旧是  按照前台department/addPosition.htmls继续找下去,若是想在此打住,不要再去前台了,添加注解spa

1 @RequestMapping("addPosition")
2     public void addPosition(Position position){
3         position.setCreateDate(new Timestamp(System.currentTimeMillis()));
4         position.setUpdateDate(new Timestamp(System.currentTimeMillis()));
5         //操做人  未插入
6         positionService.save(position);
7     }
View Code

 

更改以后以下:3d

1 @RequestMapping("addPosition")
2     @ResponseBody
3     public void addPosition(Position position){
4         position.setCreateDate(new Timestamp(System.currentTimeMillis()));
5         position.setUpdateDate(new Timestamp(System.currentTimeMillis()));
6         //操做人  未插入
7         positionService.save(position);
8     }
View Code

 

 

问题2:

在此基础上,又发现一种新的状况:

后台代码以下:

 1 @RequestMapping("verifyFormula")
 2     @ResponseBody
 3     public void verifyFormula(String formula){
 4         InfixInToSuffix is = new InfixInToSuffix();
 5         String a = null;
 6         try {
 7             if(is.userPattern(formula)){
 8                 a = is.toSuffix(formula);
 9             }
10         } catch (Exception e) {
11             System.out.println("公式有问题");
12         }
13     }
View Code

或者:

 1 @RequestMapping("verifyFormula")
 2     @ResponseBody
 3     public String verifyFormula(String formula){
 4         InfixInToSuffix is = new InfixInToSuffix();
 5         String a = null;
 6         try {
 7             if(is.userPattern(formula)){
 8                 a = is.toSuffix(formula);
 9             }
10         } catch (Exception e) {
11             System.out.println("公式有问题");
12         }
13         return a;
14     }
View Code

 

这两种状况,虽然前台js中使用ajax访问了后台,可是后台方法处理完

1.void没有返回值  

2.虽然有返回值,可是String a = null;可能会直接将这个a返回,可是a初始化就是Null,也就是没有开辟实际的空间,这样也是返回不到ajax的回调函数中的!!!!!

多注意这两种状况!!

 

正确处理这种状况,应当:

 1     @RequestMapping("verifyFormula")
 2     @ResponseBody
 3     public String verifyFormula(String formula){
 4         InfixInToSuffix is = new InfixInToSuffix();
 5         String a = "";
 6         try {
 7             if(is.userPattern(formula)){
 8                 a = is.toSuffix(formula);
 9             }
10         } catch (Exception e) {
11             System.out.println("公式有问题");
12         }
13         return a;
14     }
View Code

 

最起码的给String a = "";便可!!

 

 

 

问题3:

一样在controller处理完后,先后台都没有报错,可是也是没有进入ajax回调函数

后台错误代码展现:

 1 @RequestMapping(value = "boundWx" ,produces = "text/json;charset=UTF-8")
 2     @ResponseBody
 3     public String boundWx(String name,String password){
 4         List<Member> members = new ArrayList<Member>();
 5         Member member = memberService.findByUsername(name);
 6         if(member == null){
 7             member = memberService.findByMobile(name);
 8             if(member == null){
 9                 members = memberService.findListByEmail(name);
10             }
11         }
12         if(members.size() > 0){
13             member = members.get(0);
14         }
15         if(member != null){
16             if(DigestUtils.md5Hex(password).equals(member.getPassword())){
17                 return "wx/member/index.jhtml";
18             }else{
19                 return "密码有误";
20             }
21         }else{
22             return "用户信息有误";
23         }
24     }
View Code

 

问题解决:

由于这个方法中 返回给前台后是有乱码出现的,因此加了:@RequestMapping(value = "boundWx" ,produces = "text/json;charset=UTF-8")

而问题就出在:此处的produces = "text/json;charset=UTF-8"与返回值的格式并不相符。

 

更改成以下的就能够正常返回了:

@RequestMapping(value = "boundWx" ,produces = "text/html;charset=UTF-8")

 

问题4:

新的同类型问题

ajax + springMVC后台处理完成跳转给前台的ajax的回调函数中,

表现:后台程序执行了三次,可是最后都不会返回到前台回调函数中,且先后台都不报错!

问题:请认真检查前台使用了ajax的是在哪一个按钮的点击事件中,这个点击事件是否 return ; 请认真检查前台jsp中是否重复引用了jQuery等js文件致使后台会重复执行几回

 

问题5:

依旧是ajax + springMVC后台处理完成跳转给前台的ajax的回调函数中,

虽然前台返回状态是200,请求成功,可是始终不进入ajax的success回调方法。

问题:检查后台接口是否是返回的是null,也就是return null;

由于即便状态是200.可是只能表明先后台是联通的,可是不表明返回的参数是有值的,若是return null;那么回到前台之后,判断success字段值若是没有值,固然会进入error的回调函数,而不会进入success的回调函数。

相关文章
相关标签/搜索