0

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.

mambo
  • 5
  • 3
  • This seems an issue on how you are using jquery and the ajax authentication. Please check these answers: http://stackoverflow.com/questions/5507234/how-to-use-basic-auth-and-jquery-and-ajax specially about the username/password parameters – adosaiguas Nov 24 '14 at 12:18
  • You should open the index.html from server not a local file. – Mika Tuupola Nov 26 '14 at 15:52
  • Thank for your reply, but this is a wep app with cordova. Index.html is in webapp on Android device. – mambo Nov 27 '14 at 19:32

0 Answers0