0

I need to call this patch method from Javascript. But the request's body is not seted. Always is void. Is like something more need to be seted at the $.ajax parameters to send the request body. I know the issue is at the body, because if I set at server side the bady as mandatory, the call works, also works if I change just the call type to "POST". But I need a Patch with body.

At server side is working fine (JAVA and Spring):

@PatchMapping
@ResponseStatus(code = HttpStatus.OK)
@Operation(description = "Update User record")
ResponseEntity<UserDTO> updateUser(
    @PathVariable("userId") @Parameter(description = "User", required = true, in = ParameterIn.PATH) Long userId,
    @NonNull @RequestBody UserDTO userDTO);
}

At JS side (The error for sure is here):

var url = ...
var dataStatus = {"status": "A"};
$.ajax({
    url : url,
    type : 'PATCH', 
    contentType : 'application/json',
    processData: false,
    dataType: 'json'
    async : false,
    cache:          false,
    crossDomain:    true,     
    data : JSON.stringify(dataToSend),
    success : function(data) {
        if (data == null) {
            console.error("Error");
        }
        
        if (callback) {
            callback(data);
        }
    },
    error : function(error) {
        console.error("Ajax call error occurs during...");
    },
    beforeSend : function(xhr) {
        debugger;
        xhr.setRequestHeader('Authorization', 'Bearer ' + sessionStorage.accessToken);
        xhr.setRequestHeader('Cache-Control', 'no-cache');
        xhr.setRequestHeader('X-Xss-Protection', '1; mode=block');     
    }
});

The call works but the request's body allways is null. But if I change the call type to "PUT" or "POST" the request's body is setted correctly, but I need a "Patch" call. Must I add any other http header or change something?

Thanks!

JMG
  • 13
  • 5
  • Does this help you? https://stackoverflow.com/questions/13642044/does-the-jquery-ajax-call-support-patch – Thallius Dec 05 '21 at 09:14
  • Does not help me, I have not any error. The call works but the request's body allways is null. But if I change the call type to "PUT" or "POST" the request's body is setted correctly. Thanks. – JMG Dec 05 '21 at 18:45

0 Answers0