2015-08-28 103 views
0

Ajax调用:Ajax调用总是用SpringMVC返回错误

$.ajax({ 
    type:'post', 
     url:'https://hybris.local:9002/store/verify?productCodePost='+productid, 
     data : {notifyemail : notifyemail}, 
     dataType : "text", 
     success : successmethod, 

     error : function(data, status) { 
      //alert("Error "+status); 

      $('#showbecomepartnerMessage').show(); 

     } 
}); 

alert("test values are"+notifyemail); 

document.getElementById('notifyemail').value=''; 

} 

function successmethod(data) { 

    if (data != null) { 
     alert('Success'); 
     $('#showemailMessage').show(); 
    } else { 
     alert('Error'); 
    } 

} 

控制器:

@RequestMapping(value = "/verify", method = RequestMethod.POST, produces = "application/json") 
    public String verifyEmail(@RequestParam("productCodePost") final String code, final Model model, 
      @Valid final AddToCartForm form) 
    { 


     System.out.println("Inside Verify method"); 
     final String email = form.getNotifyemail(); 
     System.out.println("Email is " + email); 
     System.out.println("Product code is== " + code); 
     final Boolean status = true; 

     if (email != null) 
     { 

      System.out.println("Email id is" + email); 

      notifyStockEmail(email, code); 


     } 


     if (status.booleanValue()) 
     { 
      System.out.println("value of Boolean " + status.booleanValue()); 
      //return "success"; 
      model.addAttribute("success", "success"); 
     } 
     else 
     { 

      //return "fail"; 
      model.addAttribute("error", "error"); 
     } 
     return "success"; 
    } 

在上面的代码中,我正在做一个Ajax调用并调用控制器“/验证”,并从控制器我返回一个布尔值为true,但每次在jsp中执行错误方法而不是成功method.So如何通过传递来自控制器的真实值如上调用成功方法。任何帮助将不胜感激。

+2

什么正在由服务器返回的错误? – David

+0

你是否使用spring安全保护应用程序? –

+0

@David内部控制台重新调用的值为true ..但在ajax函数中调用错误方法... – User2413

回答

0

请试试这个:

@RequestMapping(value = "/verify", method = RequestMethod.POST) 
public @ResponseBody String verifyEmail(@RequestParam("productCodePost") final String code) { 

    //do what you want and return your status 
    return "OK"; 
} 

你Ajax调用应该是这样的:

$.ajax({ 
    type:'post', 
     url:'https://hybris.local:9002/store/verify?productCodePost='+productid 
}).success(function(data) { 
    successmethod(data); 
}); 
+0

这里如何将数据'data:{notifyemail:notifyemail}'发送给控制器? – User2413

0

尽管您断言,您没有返回布尔值。您在模型中放置了一些名为'success'和'error'的字符串属性并转发到名为“success”的视图,该视图可能存在或不存在。

如果您只是想将布尔值true | false写入响应流,那么您可以按如下所示进行操作。

添加注释@ResponseBody:

@RequestMapping(value = "/verify", method = RequestMethod.POST, produces = "application/json") 
public @ResponseBody boolean verifyEmail(@RequestParam("productCodePost") final String code, final Model model, 
      @Valid final AddToCartForm form){ 

    boolean status = false; 

    //check and set status. 

    return status; 
} 

参见:

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-responsebody

这个注解可以放在一个方法,并且指示返回 类型应该写入直HTTP响应正文(而不是 放置在模型中,或解释为视图名称)

+0

查看更新的答案。 –

+0

可以详细说明状态码,因为它显示错误来初始化布尔变量,.. – User2413

+0

final boolean status = false; \t \t如果(电子邮件!= NULL) \t \t { \t \t \t的System.out。println(“电子邮件ID是”+电子邮件); \t \t \t notifyStockEmail(email,code); \t \t} \t \t如果(状态) \t \t { \t \t \t model.addAttribute( “状态”, “成功”); \t \t} \t \t其他 \t \t { \t \t \t //返回 “不及格”; \t \t \t model.addAttribute(“status”,“error”); \t \t} \t \t return“status”;这是设置状态的正确方法吗? @Alan Hay – User2413