【发布时间】:2012-07-14 14:14:25
【问题描述】:
我正在尝试向 servlet 发送 POST 请求。请求是通过 jQuery 以这种方式发送的:
var productCategory = new Object();
productCategory.idProductCategory = 1;
productCategory.description = "Descrizione2";
newCategory(productCategory);
newCategory 在哪里
function newCategory(productCategory)
{
$.postJSON("ajax/newproductcategory", productCategory, function(
idProductCategory)
{
console.debug("Inserted: " + idProductCategory);
});
}
而 postJSON 是
$.postJSON = function(url, data, callback) {
return jQuery.ajax({
'type': 'POST',
'url': url,
'contentType': 'application/json',
'data': JSON.stringify(data),
'dataType': 'json',
'success': callback
});
};
使用 firebug 我看到 JSON 发送正确:
{"idProductCategory":1,"description":"Descrizione2"}
但我得到 415 Unsupported media type。 Spring mvc 控制器有签名
@RequestMapping(value = "/ajax/newproductcategory", method = RequestMethod.POST)
public @ResponseBody
Integer newProductCategory(HttpServletRequest request,
@RequestBody ProductCategory productCategory)
前几天有效,现在无效。如果需要,我会显示更多代码。 谢谢
【问题讨论】:
-
从前几天你有什么变化?另外,
var productCategory = { idProductCategory: 1, description: "Descrizione2" };不是更简洁更易于阅读吗?您需要告诉 Spring 专门接受application/json吗?换句话说,它是否期望数据以某种形式出现? -
自从我在这个项目的其他部分工作以来,我做了很多事情,今天我发现了这个回归。在这部分我没有改变任何东西。是的,我必须使用这种方式,因为我从表单中获取输入。
-
不,您不是,您是从 JSON Ajax 帖子中获取的,这与表单编码数据不同。
-
您确定 Jackson 在您的 CLASSPATH 中仍然可用吗?
-
如果你发送 text/json 而不是 application/json 你会得到同样的错误
标签: ajax json spring post http-status-code-415