1.-我想在我的控制器中回忆这两个这样的参数:
* / Controller
@RequestMapping(value = "/insertCatalogGeneric", method = RequestMethod.POST)
public @ResponseBody
String insertCatalogGeneric(@RequestBody CatalogGeneric catalogGeneric , String tableName) {
String table_name = tableName;
String catalogInserted = catalogGenericService.insertCatalogGeneric( table_name, catalogGeneric);
return catalogInserted;
}
2.-我以这种方式发送给控制器...
$.ajax({
url : 'insertCatalogGeneric',
type : 'POST',
data: {'catalogGeneric' : JSON.stringify(description_data) , 'tableName' : "fadsf"},
async: true,
cache: false,
contentType: "application/json; charset=utf-8",
success:function(data, textStatus, jqXHR) {
3.-在我的调试中我收到错误400没有找到方向....我知道这是我发送参数的方式,但是这样做的正确形式是什么?
答案 0 :(得分:0)
在你的情况下,Ajax调用contentType:“application / json”格式,所以当你用JSON格式进行ajax调用时,在你的Controller中你的Request Mapping应该有consumes = "application/json"
,
对于特定请求映射,在控制器中添加如下内容,
@RequestMapping(value = "/insertCatalogGeneric", method = RequestMethod.POST,
consumes = "application/json")
public @ResponseBody
String insertCatalogGeneric(@RequestBody CatalogGeneric catalogGeneric , String tableName) {
答案 1 :(得分:0)
[附加] 对于安全请求正文格式 尝试将整个JavaScript对象进行字符串化。
$.ajax({
url : 'insertCatalogGeneric',
type : 'POST',
data: JSON.stringify({'catalogGeneric' : description_data , 'tableName' : "fadsf"}),
cache: false,
contentType: "application/json; charset=utf-8",
success:function(data, textStatus, jqXHR) {
}
});