The client sends a request to the REST server to delete the record. Angular Performance

$scope.deleteItem = function(id) { var url = 'http://localhost:8080/api/message/' + id; var config = { headers: {'Content-Type': 'application/json'}}; $http.delete(url, config).then(function(response){ console.log(response); }, function(response){ console.log(response); }); 

Spring server side boot

  @RequestMapping( value = "api/message/{id}", method = RequestMethod.DELETE, consumes = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<?> deleteMessage(@PathVariable Long id) { if (!messageService.delete(id)) { throw new DataNotFoundException("Data with id=" + id + " not found."); } return ResponseEntity.ok("Successfully deleted user"); } 

Returns the text, this is a stub, in fact, I do not know yet what to return correctly in this case, according to the true idea, although Angular can check the answer and with the line == "done". In general, if you give extra. advice then thanks.

Now I get a response on the server side:

Resolved exception caused by Handler execution: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'null' not supported

Browser console log:

 DELETE http://localhost:8080/api/message/1 415 config: Object data: Object headers: (d) status: 415 statusText: "" 

What am I doing wrong?

  • Perhaps you have this "api/message/{id}" null "api/message/{id}" ? Deletion from the database occurs? - JVic
  • @GenCloud I honestly do not understand what the error is there, the output url variable is localhost: 8080 / api / message / 1 (or another id). - Sergei R
  • @Victor does not delete. - Sergei R

2 answers 2

1) a request for a mapper is never written like this 2) a link to a specific requester controller should look like - / api / message / + id 3) take a look at my example of user registration, should help you with your example

  $.ajax({ type: "POST", url: "/api/account", data: JSON.stringify(reg), contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { $.gritter.add({ title: '<i class="fa fa-info-circle"></i> Поздавляем', text: 'Вы успешно зарегистрированы!', sticky: true, time: '', class_name: 'gritter-info' }); setTimeout(function() { window.location = "/login.jsp"; }, 2000); }, error: function (jqXHR, status) { $('.login-wrapper').removeClass('fadeOutUp'); var data = jqXHR.responseJSON; $.gritter.add({ title: '<i class="fa fa-times-circle"></i> Ой, произошла ошибка!', text: data.msg, sticky: false, time: '', class_name: 'gritter-danger' }); } }); 

    My solution: server side

     //Delete the item @RequestMapping( value = "api/message/{id}", method = RequestMethod.DELETE) public ResponseEntity<?> deleteMessage(@PathVariable Long id) { if (!messageService.delete(id)) { throw new DataNotFoundException("Data with id=" + id + " not found."); } return new ResponseEntity<>(HttpStatus.OK); } 

    client side

     $scope.deleteItem = function (id) { $http.delete($scope.url + 'message/' + id).success(function (response) { делаем что то, например удаляем из массива или перезагружаем страницу }, function (response) { console.log("Return error"); }); }