1

I am calling a web service with a jQuery Ajax request and I need to pass an authorization header. So I have something like the below...

  $.ajax
        ({
            type: "GET",
            url: "https://emmawatson.com/askForaDate",
            dataType: 'json',
            beforeSend: function (xhr) {
                xhr.setRequestHeader('Authorization', 'Basic YXBpdHVybnRvQGFwaS5jb206QXBpMzIxJDEyMw');
            },
            success: handleData
        });

When I look at the request headers in the outgoing request, I only see this..

 Access-Control-Request-Headers:authorization
 Access-Control-Request-Method:GET

I was expecting to see something like this..

 authorization:Basic YXBpdHVybnRvQGFwaS5jb206QXBpMzIxJDEyMw

So it looks like the authorization request header is not being sent.

I also tried the other approaches suggested in this post and this one.

Could anyone tell me what I am missing? How do I get the actual header value to show up in the request headers?

Community
  • 1
  • 1
AbuMariam
  • 3,282
  • 13
  • 49
  • 82
  • From which url are you make the call? Do you have any error in console? If so could you paste here please? – borracciaBlu May 05 '17 at 00:07
  • 1
    the outgoing request with those `Access-Control-Request` headers will be `OPTIONS` (CORS preflight) - the Authorization header will only be sent once the preflight **succeeds** - which I'm guessing is not the case - in the GET request that follows a successful preflight check. This is standard CORS behaviour – Jaromanda X May 05 '17 at 00:16

1 Answers1

0

I know this is an old question but I believe that jQuery will only supply the credentials if a challenge is received, like what Jaromanda said in their comment above.

Mike Devenney
  • 1,758
  • 1
  • 23
  • 42