Java 使用Ajax请求时,HttpMediaTypeNotSupportedException
我有两个类似的功能,一个正常工作,另一个不正常。 有些表单有一个带有Java 使用Ajax请求时,HttpMediaTypeNotSupportedException,java,jquery,ajax,spring-mvc,spring-restcontroller,Java,Jquery,Ajax,Spring Mvc,Spring Restcontroller,我有两个类似的功能,一个正常工作,另一个不正常。 有些表单有一个带有onclick=“save()”的按钮。此表单用于创建和更新数据。因此,方法save()是: function save() { if($('input#id).val() !== '') { ctx.ajaxUrl = ctx.ajaxUrl + 'update'; } const form = $("#detailsForm"
onclick=“save()”
的按钮。此表单用于创建和更新数据。因此,方法save()
是:
function save() {
if($('input#id).val() !== '') {
ctx.ajaxUrl = ctx.ajaxUrl + 'update';
}
const form = $("#detailsForm");
$.ajax({
type: "POST",
url: ctx.ajaxUrl,
data: form.serialize()
}).done(function () {
$("#editRow").modal("hide");
updateTable();
successNoty("Saved");
});
}
如果此表单中有一个新对象,我必须创建(并保存在应用程序中),则带有id=“id”
的标签input
为空。这个功能运行良好。但是,如果我必须更新现有对象,则带有id=“id”
的标记input
具有一些值,并且操作符if
中的代码被执行,并且ctx.ajaxUrl
被更改。正是此更改导致AbstractHandlerExceptionResolver.logException:207-已解析[org.springframework.web.HttpMediaTypeNotSupportedException:内容类型'application/x-www-form-urlencoded;charset=UTF-8'不受支持]
RestController中有创建和更新的方法:
@RestController
@RequestMapping(value = "/profile/meals", produces = MediaType.APPLICATION_JSON_VALUE)
public class MealUIController extends AbstractMealController {
...
@PostMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void create(@RequestParam LocalDateTime dateTime,
@RequestParam String description,
@RequestParam int calories) {
super.create(new Meal(dateTime, description, calories));
}
@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.NO_CONTENT)
public void update(@RequestBody Meal meal) {
super.update(meal, meal.id());
}
}
请帮助我理解url更改导致此异常的原因