0

我正在发送一个带有 json 的 POST 请求,如下所示:

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

Network当我通过传递以下有效负载查看此请求时

{"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 数据提供一个参数名称。

4

1 回答 1

0

那这个呢?由于 json 只是一个字符串,并且您正在发布数据,这应该可以工作。

 $.ajax({
     type: "POST",
     url: "myurl.htm",
     contentType: "application/json",
     data: 'mydata={"val1":"something","stuff":[{"val1":"value1","data":"Someone"}]}',
     dataType: "text"
}); 
于 2012-11-18T14:34:27.047 回答