1

I am using this code:-

  var saveData=$.ajax({
                            url: "api",
                            headers: {
                                "Access-Control-Allow-Origin":"*",
                                "Access-Control-Request-Headers": "x-requested-with",
                                 "Access-Control-Allow-Methods": "GET, POST, PATCH, PUT,DELETE,OPTIONS",
                                 "Access-Control-Allow-Headers": "Origin, Content-Type, X-Auth-Token",
                                 "Accept":"application/json"
                                    },
                    type: 'POST',
                         dataType: 'application/octet-stream',
    contentType: 'image/jpeg',
    processData: false,
    data: binaryData,
    success: function (data) {
      alert(data);
    },
    error: function(){
      alert("Cannot get data");
    }
});

But I am getting this:- Access to XMLHttpRequest at 'api/bintest1.jpeg' from origin 'null' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

But it is working fine in postman:- enter image description here

enter image description here

gourabk
  • 177
  • 1
  • 12
  • 1
    Possible duplicate of [Why does my JavaScript code get a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error when Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-get-a-no-access-control-allow-origin-header-is-pr) – Tyler Roper Oct 27 '19 at 04:36
  • But I have given the headers – gourabk Oct 27 '19 at 04:38
  • Please could you help me out .Thanks in advance.I am not getting solution and I am stuck – gourabk Oct 27 '19 at 04:40
  • @gaurabk These headers are response headers. There is no use in sending them as request headers. The changes should be made on server to send these response headers. – Nithin Thampi Oct 27 '19 at 06:37
  • But i have allowed CORS in api gateway @nithin – gourabk Oct 29 '19 at 18:08
  • @gourabk You have enabled CORS only for `GET` requests. – Nithin Thampi Oct 31 '19 at 10:26

0 Answers0