Jquery Ajax POST无法正常工作。适用于GET

时间:2015-03-30 15:37:05

标签: javascript jquery ajax spring-mvc

我正在尝试发布jquery Ajax请求,但它没有发送到我的MVC控制器。

$.ajax({
            url : contextPath + "/check/panelnumber",
            type : "POST",
            contentType : "application/json",               
            data :{
                    "execution" : execution,
                    "panelNumber" : panelNumber
            },
});

Spring MVC控制器

@Controller
@RequestMapping("/check")
public class ValidateFieldValueController extends FlowEnabledBaseController {

@RequestMapping(value = "/panelnumber", method = RequestMethod.POST)
public ResponseEntity<String> checkPanelNumber(HttpServletRequest request,
        HttpServletResponse response,
        @RequestParam("panelNumber") String panelNumber,
        @RequestParam("execution") String execution) {
......
Gson gson = new Gson();
return new ResponseEntity<String>(gson.toJson(details), HttpStatus.OK);
}

然而,GET方法的效果非常好! 尝试添加dataType:&#34; json&#34;同样,但即使GET呼叫停止工作。浏览器控制台显示错误为HTTP 400错误请求,但是当通过firefox插件检查时,POST参数正常。 有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

对于@RequestParam,POST正文和JSON内容类型,Spring有点敏感:它只是不会解析它们。您可以通过以下三种方式解决此问题:

  1. 将内容类型更改为application/x-www-form-urlencoded
  2. 将两个@RequestParam更改为一个@RequestBody Map<String, Object> body,然后使用Map手动解析您的参数。
  3. 设置一个自定义Argument Resolver,可以解析JSON以获得所需的值。
  4. 第二种方法可能最容易改变,但它会让你失去一些Spring为你做的自动验证。

相关问题