Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Ajax请求时,HttpMediaTypeNotSupportedException_Java_Jquery_Ajax_Spring Mvc_Spring Restcontroller - Fatal编程技术网

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更改导致此异常的原因