ajax auth基本苗条

时间:2014-11-24 00:28:28

标签: ajax basic-authentication slim

我遇到HTTP Basic Auth,Ajax和SLIM的问题。

index.html在我的电脑上:

$.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"); 
}*/
});

我的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(苗条):

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();

它可以在Chrome https://ws2-bots.faistescourses.fr/admin中使用test / test 但它在index.html中不起作用:错误:XMLHttpRequest无法加载https://ws2-bots.faistescourses.fr/admin。 Chrome中的HTTP状态代码401无效。

请帮助。感谢。

0 个答案:

没有答案