0
var settings = {
  "url": "url link",
  "method": "DELETE",
  "timeout": 0,
};

$.ajax(settings).done(function (response) {
  console.log(response);
});

The above code was generated from postman. When I send a request via postman app, it works; however, when I post the same code generated from postman on my app, it throws an error.

 $.ajax({
  url: "url link",,
    type: 'DELETE',
    headers: {
              'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept, Z-Key',
          'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
      "Content-type": "application/json",
      "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36"
      
          },
    success: function(result) {
      console.log("deleted");
        console.log(result);
    }
});

I have even tried the above code by setting 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS'. But I still receive the same error: "Method DELETE is not allowed by Access-Control-Allow-Methods in preflight response."

Sanjan
  • 11
  • 2
  • CORS headers have to go on the response. It would be really pointless if you could grant yourself permission to access other people's websites! – Quentin Apr 04 '22 at 15:28
  • Yup... CORS headers are sent by the server, not by the user who makes the request. – Pipe Apr 04 '22 at 15:29

0 Answers0