在jquery jax POST数据的Ajax请求中设置参数名称

时间:2012-11-18 14:29:48

标签: jquery ajax spring spring-mvc

我正在用json发送一个POST请求:

$.ajax({
     type: "POST",
     url: "myurl.htm",
     contentType: "application/json",
     data: '{"val1":"something","stuff":[{"val1":"value1","data":"Someone"}]}',
     dataType: "json"
}); 

当我从Network查看此requet时,它传递了以下有效载荷

{"val1":"something","stuff":[{"val1":"value1","data":"Someone"}]}

但是,我希望请求加载为:

mydata: {"val1":"something","stuff":[{"val1":"value1","data":"Someone"}]}

因为我的控制器正在接受以下内容:

 @RequestMapping(headers ={"Accept=application/json"},value="/myurl.htm", method= RequestMethod.POST)
 public ModelAndView mymethod(
      @RequestParam(value="mydata", required=false) String mydata {
  logger.info(mydata);
}

所以我需要一种方法来简单地给我作为POST请求发送到服务器的json数据一个参数名称。

1 个答案:

答案 0 :(得分:0)

这是怎么回事?由于json只是一个字符串而你发布的数据应该可行。

 $.ajax({
     type: "POST",
     url: "myurl.htm",
     contentType: "application/json",
     data: 'mydata={"val1":"something","stuff":[{"val1":"value1","data":"Someone"}]}',
     dataType: "text"
});