Bearer token through ajax headers not working


#1

Hey guys,
as i try to add Header to my ajax request i get no server response,
in google chrome i get this error "net::ERR_SPDY_PROTOCOL_ERROR"
and in
mozzila firefox i get no response,
as i checked in mozzila console there is request method OPTION with no status code,
but as i remove http headers(xhr.setRequestHeader()) i get the response

I am using free hosting plans of 000webhost for testing purposes ,
please help me to solve this problem

here is my ajax code

const url = "https://xyspace.000webhostapp.com/secret.php"
    let xhr = new XMLHttpRequest()
    xhr.open('POST',url,true)

    let my_token = 'idjzoi59599frkdijzjd'
    xhr.setRequestHeader("Authorization","Bearer " + my_token)
    xhr.send()
  
    xhr.onreadystatechange = function() {
      if(xhr.readyState == 4 && xhr.status == 200) {
        console.log(xhr.responseText)
      }else{
        localStorage.removeItem('private-token')
      }
  }
  xhr = null

#2

@Supun @Infinity @teodor


#3

Hi @extremety1989

Please read the following topic for more information:

I am afraid your only choice is to use jQuery in order to perform XHR, as 000webhost does not allow OPTIONS method on free plans.