java - Ajax 请求时出现错误 415(Ajax、Spring)

标签 java jquery json ajax spring-mvc

我向服务器发送 Ajax 请求,但服务器没有得到响应。

PUT 请求:

function editNavigation() {
        var flight={
            id:idAction.replace('edit',''),
            navigation:newNavigation
        };
        console.log(flight);
        var prefix = '/airline/';
        $.ajax({
            type: 'PUT',
            url: prefix +'flights/' + idAction.replace('edit',''),
            data: JSON.stringify(flight),
            headers:{contentType: "application/json"},
            success: function(receive) {
                $("#adminTable").empty();
                $("#informationP").replaceWith(receive);
                $("#hiddenLi").removeAttr('style');
            },
            error: function() {
                alert('Error edited flight');
            }
        });
    }

Controller :

    @RequestMapping(value = prefix + "/flights/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String updateFlight(@PathVariable("id") String id, @RequestBody FlightDto flightDto) {
        String returnText = "Flight edited successful";
        String str1 = flightDto.getNavigation();
        logger.info(str1);
        String str2 = id;
        logger.info(str2);
        return returnText;
    }

浏览器控制台:

Object {id: "5", navigation: "sdf"}

PUT http://localhost:8080/airline/flights/5 415 ()

为什么我收到错误 415? 为什么我的 Controller 不发送响应?

最佳答案

415 共振代码是不支持的媒体类型

  1. 如果您的服务需要 POST(或任何类型)请求并且用户发送 PUT 请求,然后服务器将给出这种响应。

  2. 如果用户发送错误的MediaType,也会出现此类错误。

在您的代码中,我认为您配置了错误的MediaType,即APPLICATION_JSON_VALUE。您可以指定MediaType.APPLICATION_JSON

我认为这会有所帮助。

关于java - Ajax 请求时出现错误 415(Ajax、Spring),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44097892/

相关文章:

java - 在大约 100 个字符和下一个符号后分割字符串 (Java)

javascript - 嵌套复杂 JSON 中的搜索键

JavaScript 不会改变背景图像

java - "Cannot deserialize instance of"来自外部API

json - 使用 webpack 为 react-lottie 从主包中排除 JSON 文件

java - 为什么 Java 中的后缀运算符从右到左求值?

java - 如何修复 android.support 和 firebase 错误?

java - 如何使 JavaFX 2.0 中的类成为可用的节点?

jQuery "hide"函数不起作用

安卓 : Synchronous way to use asyncttask : Json upload issue