0

I have the HTML file and try to send the request from that page with JavaScript or jQuery, but it doesn't send the "header" to the server and it gives me the error 405. I've read related questions and try out some of them but the error code was the same. I use Fiddler to see the final request and in that there is no header as well as the method change to the OPTION! some of the codes I used:

$.ajax({
                url: "URL",
                type: "GET",
                beforeSend: function(xhr){xhr.setRequestHeader('Authorization', 'Bearer  HASHKEY');},
                success: function() { alert('Success!'); }
            });

or

  var xhttp = new XMLHttpRequest();
            xhttp.open("GET", "URL", true); 
            xhttp.setRequestHeader("Authorization", "Bearer HASHKEY");
            xhttp.setRequestHeader('Content-Type', 'text/plain');
            xhttp.setRequestHeader('Accept', 'application/json');
            xhttp.send();
            xhttp.onreadystatechange = function () {
                var response = xhttp.responseText;
                if (xhttp.readyState == 4 && xhttp.readyState == 200) {
                var obj = JSON.parse(response);

                    // handle data as needed...
                    alert("obj" +obj);
                }
            };

Totally speaking I'm looking for a piece of code which can call API from the HTML which is content header: authorization.

Cheers

FARzad
  • 3
  • 3

1 Answers1

0

Try this:

$.ajax({ 
    url: "URL", 
    type: "GET", 
    ‎headers: { 
        ‎"Authorization": "Bearer HASHKEY" 
    ‎}, 
    success: function() { alert('Success!'); } 
});

Also, check that your server configuration supports CORS and explicitly allows the Authorization header.

jeprubio
  • 17,312
  • 5
  • 45
  • 56
  • Try adding the Access-Control-Allow-Headers and Access-Control-Allow-Origin headers on your server: https://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work/10636765#10636765 – jeprubio Dec 01 '17 at 05:25