使用jquery无法正确处理控制器的响应

时间:2016-12-17 19:04:36

标签: jquery spring-restcontroller

在具有rest架构的spring boot应用程序中

我尝试做一个ajax调用

jQuery.ajax({
    type: "PATCH",
    url: getHostName() + "/members/" + memberId + "/contracts/" + contractId + "?status=CANCEL",
    contentType: "application/json",
    dataType: 'json',
    headers: {
      "Authorization": "Basic " + $.cookie('authorization')
    },
    success: function (data, status, jqXHR) {
      $('#modalSaveSuccess').modal('show');
    },
    error: function (jqXHR, status) {
      checkError(jqXHR);
    }
});

在控制器中,

 @PatchMapping(value = "/members/{memberId}/contracts/{contractId}")
 public ResponseEntity updateContractStatus(@PathVariable("memberId") Long memberId, @PathVariable("contractId") Long contractId, @RequestParam("status") StatusEnum status) {

     if (status == StatusEnum.CANCEL) {
         contractService.cancelContract(contractId);
     }
     return new ResponseEntity(HttpStatus.OK);
 }

当我调试时,我看到返回HttpStatus.OK

在javascript中进入错误部分,状态为200且状态为文本" parsererror"

回复标题

HTTP/1.1 200
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: 0
X-Frame-Options: DENY
Content-Length: 0
Date: Sat, 17 Dec 2016 18:53:13 GMT

1 个答案:

答案 0 :(得分:1)

您已设置dataType: 'json',但您没有返回json。请删除该行并检查结果。