![](/img/trans.png)
[英]Error: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'text/plain;charset=UTF-8' not supported
[英]Unsupported Media Type“,”exception“:”org.springframework.web.HttpMediaTypeNotSupportedException“,”message“:”Content
我有一個帶有以下郵政編碼的RestController,並嘗試在我的AJAX中使用它在數據庫表中添加數據。 我有以下錯誤。
RestController方法發布
@RequestMapping(method = RequestMethod.POST)
public void create(@RequestBody MessageChat chatmessage) {
messageService.save(chatmessage);
}
阿賈克斯電話
$( "#vut" ).on( "click", function(e){
e.preventDefault();
e.stopPropagation();
$.ajax({
url: '/messagechat',
method: 'POST',ContentType:'application/json',dataType: 'JSON',
data: {
message: 'a'
}
})
});
錯誤
{"timestamp":1498660502132,"status":415,"error":"Unsupported Media Type","exception":"org.springframework.web.HttpMediaTypeNotSupportedException","message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported","path":"/messagechat"}
有人可以幫忙嗎?
它應該是
$( "#vut" ).on( "click", function(e){
e.preventDefault();
e.stopPropagation();
$.ajax({
url: '/messagechat',
method: 'POST',
dataType: 'json',
data: JSON.stringify({message: 'a'})
});
});
在您的控制器中:
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public void create(@RequestBody MessageChat chatmessage) {
messageService.save(chatmessage);
}
根據jQuery函數ajax()
文檔,應將屬性名稱從ContentType
更改為contentType
。 JavaScript標識符不區分大小寫。
嘗試使用“ c ontentType”代替“ C ontentType”
嘗試
@RequestMapping(method = RequestMethod.POST, headers = "Accept=*/*)
另外,您也可以將其設置為headers =“ Accept = application / json”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.