私はAngularJSプロジェクトを作成しています。現在、http://localhost
にlaravelバックエンドでhttp://api.localhost
というプロジェクトが作成されています。どちらもnginxサーバーによって提供されています。Laravel + AngularJS CORS not working
$ http.post要求を行う際に、角度最初はCORS OPTIONSを呼び出すになり、私は適切なヘッダで応答する私のnginxのサーバーを構成した:
location/{
add_header "Access-Control-Allow-Origin" "*";
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Methods" "GET,POST,DELETE,PUT,OPTIONS";
add_header "Access-Control-Allow-Headers" "Keep-Alive,User-Agent,If-Modified-Since,Cache-Control,Content-Type,Authorization";
add_header "Access-Control-Max-Age" "1728000";
if ($request_method = 'OPTIONS') {
return 204;
}
#try_files $uri $uri/ /index.html;
try_files $uri /index.php?$query_string;
}
location = /index.php {
add_header "Access-Control-Allow-Origin" "*";
add_header "Access-Control-Allow-Credentials" "true";
add_header "Access-Control-Allow-Methods" "GET,POST,DELETE,PUT,OPTIONS";
add_header "Access-Control-Allow-Headers" "Keep-Alive,User-Agent,If-Modified-Since,Cache-Control,Content-Type,Authorization";
add_header "Access-Control-Max-Age" "1728000";
if ($request_method = 'OPTIONS') {
return 204;
}
...
}
私の角度モジュールはまた、で構成されています:予想通り
.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}])
OPTIONSはリターンを呼び出す:
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Keep-Alive,User-Agent,If-Modified-Since,Cache-Control,Content-Type,Authorization
Access-Control-Allow-Methods:GET,POST,DELETE,PUT,OPTIONS
Access-Control-Allow-Origin:*
Access-Control-Max-Age:1728000
Connection:keep-alive
Date:Mon, 04 Nov 2013 02:14:16 GMT
Server:nginx/1.2.6 (Ubuntu)
しかしトン私が作る彼以降のPOST呼び出しが取り消さのステータスで失敗し、角度はJSコンソールにエラーがスローされます:私は昨夜遅くまで滞在し、この作業を得たが、私は今日もしようとしたとき、それが戻っていた
`XMLHttpRequest cannot load http://api.localhost/users/accesstokens. Origin http://localhost is not allowed by Access-Control-Allow-Origin.`
1に正方形にする。最悪の種類の問題!
何がありますか?
EDIT:問題が見つかりましたが、まだ理解できません。私は自分のnginxのアクセスログを見て、ステータスがキャンセルされていてもPOSTリクエストが実際にサーバに当たっているのを見ました。私はまた、応答が401であることを見た。私の要求が正しいことを得た後、応答は201であった。同じCANCELED状態。しかし、私がステータスを200に調整したときに、気が狂った!要求は意図どおりに機能しました。
AngularJSは、クロスオリジン要求で200ステータスのみを受け入れる理由はありますか?
nginxのバージョン? – alfonsodev
ここで同じ問題が発生する – pajooh