如何使用ajax正确地将json发送到RestController?

时间:2016-08-09 14:23:39

标签: javascript json ajax rest

我正在尝试将json发送到api,但是我收到了这个错误:

com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'status': was expecting ('true', 'false' or 'null')

这是javascript:

$("body").on('click','.btn-danger' ,(function (){
    var id = $(this).val();
    var data = {"status": "canceled",
                "response": ""};
    console.log(data);
    $.ajax ({
        type: 'PUT',
        url: '/api/pendingrequests/' + id,
        contentType: "application/json",
        data: data,
        dataType : "html"
    }).done (function () {
        $(this).hide();
    });
}));

控制器:

@RequestMapping(value = "/api/pendingrequests/{requestId}", method = RequestMethod.PUT)
    public ResponseEntity updateRequestStatus(@PathVariable Long requestId, @RequestBody HolidayRequestAction action) throws IOException {
        return updateRequestStatusService.updateRequest(requestId, action);
    }

我想从请求中收到的POJO:

public class HolidayRequestAction {
    private String status;
    private String response;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getResponse() {
        return response;
    }

    public void setResponse(String response) {
        this.response = response;
    }
}

为什么我会得到此解析异常?可能是什么问题?

1 个答案:

答案 0 :(得分:3)

您的问题包含答案:'status': was expecting ('true', 'false' or 'null')

因此您无法发送其他值'true', 'false' or 'null'

但在请求中"status": "canceled"