Ajax request with Authorization header

Asked

Viewed 2,743 times

1

I’m trying to make an ajax request but it needs a BASIC authentication, follow my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script>

    $(function(){
      $.post({
        url: link,
        contentType: "application/json",
        data:"1",
        headers: {'Authorization': 'Basic token'},
        success: function(data) {
          console.log(data)
        }, error: function(err) {
          console.log(err)
        }
      });
    });

</script>

*I put a link/token for reasons of secrecy, but they are correct. *I made this request for PHP with same token and parameter and it worked.

Can anyone help me? Returns 401 (Unauthorized) / Response for preflight has invalid HTTP status code 401.

  • 1

    remove the headers and try to use beforeSend: Function (xhr) { xhr.setRequestHeader ("Authorization", "Basic otken"); }, ?

  • i tried with beforeSend, continued the same error =/ and can’t debug because it is a private url

  • in the Devtools of Chrome (F12), in the NETWORK tab in the request is going to correct your request in the part of the headers? It may be crude, but try switching from Authorization to Uthorization.

  • I switched to Authorization and continued the same error =/ there in headers appears Access-Control-Request-Headers: Authorization,content-type , I believe Authorization is going. But for some reason it is not authenticating. It also identifies the WWW-Authenticate: Basic , the problem must be in receiving the token, I do not know

  • or else take the header from there and create another one upon the post called $.ajaxSetup({ headers: { 'Autorizaition': 'Basic token' } })

  • had tried before and tried now again, same mistake =/

  • then it can be the service that Voce is accessing, the token is correct?

  • is yes, via PHP I got the request uncompromising (with the same token)

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.