0

I am requesting cross origin domain and adding custom headers. But header is not getting in server side.

Code:

$.ajax({
    type: "GET",
    crossDomain: true,
    beforeSend: function (request) {
        request.setRequestHeader("Authorization", "Bearer " +($("#accesstoken").val()));
    },
    url: myJSonServer + encodeURI(operation),
    dataType: 'jsonp',
    cache: false,
    success: callback,
    error: function (jqXhr, textStatus, errorThrown) { alert(textStatus + ": " + errorThrown); }
});

In server side added:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: *
Access-Control-Request-Method: *
Beena Shetty
  • 3,676
  • 2
  • 28
  • 31
  • 1
    Have a look in the console. You likely die in the preflight OPTIONS call – mplungjan Nov 14 '17 at 13:05
  • 1
    your server must send a Access-Control-Allow-Headers: Authorization, for more info [link](https://stackoverflow.com/questions/14153569/can-i-set-headers-in-cross-domain-json-requests) – Onkar Nov 14 '17 at 13:22
  • 1
    From memory I think Access-Control-Allow-Origin: * is not allowed when you send credentials in the request. You have to specify an explicit origin. You can google to check that, but I'm fairly sure. – ADyson Nov 14 '17 at 14:10
  • It was due to preflight OPTIONS issue. Solved. Thanks – Beena Shetty Nov 20 '17 at 05:59

0 Answers0