私のシナリオは2つのWebサーバー1つのローカルと1つのリモートで構成されています。アクセス制御 - 許可原点angjsをPHPに
ローカルWebサーバー(Apache)は、リモートWebサーバー(Lighttpd)に対してajaxリクエストを作成するWebアプリケーションを処理します。
Ajaxリクエストはangularjs $ httpを使用します。
var req = {
method: 'POST',
url: 'http://url/myphp.php',
headers: {
'Authorization': 'Basic ' + btoa('username:password'),
'Content-Type': 'application/x-www-form-urlencoded'
},
xhrFields: {
withCredentials: true
},
crossDomain: true,
data: xmlString
}
$http(req).then(function() {
console.log("OK!");
});
リモートPHPスクリプトは次のとおりです。
<?php
echo "You have CORS!";
?>
は、残念ながら私は
401 Unhauthorized
XMLHttpRequest cannot load http://url/myphp.php. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. The response had HTTP status code 401.
リモートWebサーバが認証モードが有効の.htpasswdたとCORS要求が設定されました。あなたは、角度がPOSTを行う前に、アクセス起源ヘッダをチェックする飛行前のOPTIONS要求を行っているクロスドメインPOSTを行っているので
はのlighttpd.confsetenv.add-response-header = ("Access-Control-Allow-Origin" => "*")
何ブラウザ? – noahdotgansallo
ChromeとFirefoxの両方 – gravi
私はこの同じ問題を抱えていました。明らかに、ヘッダーにもかかわらず – noahdotgansallo