There is an api written in Yii2, when a request is sent via Rest, the client with the heading Authorization does everything right.

When requesting via xhr from an application javascript, no header is set, yii does not see it. I can not understand what the problem is

var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://myhost.loc/post?startDate=01.10.2017&endDate=30.11.2017', true); xhr.setRequestHeader('Authorization','Bearer eyJ0eXAiOiJKV1...'); xhr.send(); xhr.onreadystatechange = function() { if (this.readyState != 4) return; if (this.status != 200) { alert( 'ошибка: ' + (this.status ? this.statusText : 'запрос не удался') ); return; } } 

The javascript application is running on localhost, yii on another virtual host.

    1 answer 1

    Change to:

     xhr.setRequestHeader ("Authorization", "Basic " + btoa(username + ":" + password));