I have a problem with HTTP Basic Auth, Ajax and SLIM.
index.html is on my computer :
$.ajax({
type: 'GET',
url : 'https://ws2-bots.faistescourses.fr/admin',
success: function() { alert('Success !'); },
error: function() { alert('Failed!'); },
/* I tested 3 ways : */
/*headers: {
"Authorization": "Basic " + btoa('test' + ":" + 'test')
},*/
/*beforeSend: function (xhr) {
xhr.setRequestHeader("Authorization", "Basic " + btoa("test:test"));
},*/
/*beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic dGVzdDp0ZXN0");
}*/
});
Om my VPS : .htaccess :
Satisfy any
Options -Indexes
Header add Access-Control-Allow-Headers Authorization
Header add Access-Control-Allow-Credentials true
RewriteEngine On
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
index.php (slim) :
require_once "/var/www/vhosts/faistescourses.fr/httpdocs/ws-bots/v2/vendor/autoload.php";
$app = new \Slim\Slim();
$app->response->headers->set('Content-Type', 'application/json; charset=utf-8');
$app->response->headers->set('Access-Control-Allow-Origin', '*');
$app->response->headers->set('Access-Control-Allow-Headers', '*');
$app->response->headers->set('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
$app->add(new \Slim\Middleware\HttpBasicAuthentication(array(
"path" => "/admin",
"realm" => "Private",
"users" => array(
"test" => "test"
),
"environment" => "REDIRECT_HTTP_AUTHORIZATION"
)));
$app->get("/admin", function() use ($app){
$e = $app->request->headers->get("Authorization");
echo '{"Authorization": ' . json_encode($e) . '}';
});
$app->run();
It works in Chrome https://ws2-bots.faistescourses.fr/admin with test/test but it doesn't work in index.html :error : XMLHttpRequest cannot load https://ws2-bots.faistescourses.fr/admin. Invalid HTTP status code 401 in Chrome.
Help please. Thanks.