I am writing a To-Do List application on AngularJs, the application should have the ability to delete tasks. To do this, I need to send an appropriate request to the server from the application, but I get the status 404, while through the postman extension the status of the request comes 200. GET request with applications to the server to get the collection of all tasks passes without problems.

Request Code:

deleteTask: function (task) { $log.log(task._id); $http({ method: 'DELETE', url: 'http://localhost:3005/api/tasks/' + task._id, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).then(function successCallback(response) { $log.log(response); }); } 

Screenshot of the request from the application: enter image description here Screenshot of request from Postman extension: enter image description here

  • And without the 'Content-Type': 'application / x-www-form-urlencoded' tried? - alexoander
  • Yes, I added the content-type because I had a problem with the POST request before, it had the Form-Data object, I had to add the content-type and re-convert the form-data to urlencoded, I thought it would be possible and that something will affect, well, this is obvious The problem is not with the server, through console utilities and other software requests work with a bang. This application is just a yeoman with my added controller, factory and one view. - Andrei Shostik
  • Here it is described why this happens (the whole thing in Options) stackoverflow.com/questions/29954037/… . As actually answered below Constantine - alexoander
  • thanks, I will understand, correct - Andrey Shostik
  • Nothing) Actually, this is not quite a mistake, because This is a kind of ping for access. In your case (if you have access to the server, of course), then it is easiest to simply allow options requests. Well, if there is no such possibility, then you will have to shaman - alexoander

1 answer 1

Because the first thing Angular is sending an OPTIONS request to check the server capabilities. Therefore, you need to open this method on the server, as an example in the php header ('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'); in .htaccess is the Header line add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"

  • Thank you very much! Corrected. I immediately found a ready-made solution for me gist.github.com/nilcolor/816580 - Andrei Shostik
  • one
    Please just do not forget headers ["Access-Control-Allow-Origin"] = "*"; instead of the asterisk your ip indicate. And then somehow it will not be safe - Kostiantyn Okhotnyk