【springMVC 后台跳轉前台】1.使用ajax訪問的后台,后台正常執行,返回數據,但是不能進入前台的ajax回調函數中 ----2.前后台都沒有報錯,不能進入ajax回調函數


問題1:

使用ajax訪問的后台,后台正常執行,並且正常返回數據,但是不能進入前台的ajax回調函數中

問題展示:

 

 問題解決:

最后發現是因為后台的方法並未加注解:@ResponseBody,導致方法不認識最后返回的是給ajax的data,而是以為要去找這個頁面所以並未找到!!

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中的方法執行完成之后  不想返回前台,就此打住,則也需要加上@ResponseBody

因為即使方法返回值為void

spring也會按照前台請求過來的頁面地址去找,找不到就會如下:

 

所以,在后台:【以下的代碼依舊是  按照前台department/addPosition.htmls繼續找下去,如果想在此打住,不要再去前台了,添加注解

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

 

更改之后如下:

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的回調函數。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM