2016-12-17 38 views
0

正確處理與REST架構彈簧引導應用響應不使用jQuery

我嘗試做一個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

您已設置dataType: 'json'但不必返回JSON。請刪除該行並檢查結果。