必需的字符串参数'userName'不存在

时间:2014-12-31 18:07:57

标签: jquery spring spring-mvc

我正在尝试从java脚本控制台进行AJAX调用,如下所示

jQuery.ajax({
  contentType: "application/json",
  url: "/accountServices/resetPassword",
  type: "POST",
  data: '{
        "userName":"local_student",
        "newPassword":"test1231",
        "guid":"376ed5f7-a780-455e-8682-6b5cf45cff80"
         }'
}); 

我的控制器方法是这样的。

@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
public String resetPassword(@RequestParam("userName") String userName,
                            @RequestParam("newPassword") String newPassword,
                            @RequestParam("guid") String guid, Model uiModel, 
                            HttpServletRequest request, final Locale locale) 
                            throws MessagingException {
        try {}
}

我收到错误

必需的字符串参数'userName'不存在。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:6)

你需要改变你的

data: '{
        "userName":    "local_student",
        "newPassword": "test1231",
        "guid":        "376ed5f7-a780-455e-8682-6b5cf45cff80"
       }'

data: {
        userName:    "local_student",
        newPassword: "test1231",
        guid:        "376ed5f7-a780-455e-8682-6b5cf45cff80"
      }

没有

    ' 之前
  1. {
  2. ",同时包装 userName