私のサーバー側はC#
mvcプロジェクトです。devモードでの反応
私たちはクライアント側に反応を実装しようとします。
私はexpress
サーバーとhot-reloading
を使用して、npm
でnode-js
を使用していますので、私は私のクライアント側のコードをコンパイルするときには、http://localhost:3000上で実行されます。
ここで、サーバー側の呼び出しをいくつか追加します。 これを実行するには、別のポートのlocalhostでも開くiis expressを使用してc#
コードを実行します。 問題は、ポート:3000上のクライアントコードがlocalhost上のiis expressにajax呼び出しを行っているときに、同じドメインポリシーのために"Response for preflight is invalid (redirect)"
というエラーが発生することです。
私は間違っているのですが、サーバーとクライアントが分離しているときに、どのようにdevモードで作業するとしますか?
私は私のASP.NET
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Origin" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
編集に追加しようとしました - ソリューション
あなたが別のドメインにポストを送信するので、最初のクライアントがOPTIONS
要求を送信します。だから、解決策は、このコードを追加して、実際にある:あなたのケースでは
protected void Application_BeginRequest(object sender, EventArgs e)
{
EnableCrossOriginRequestsFromLocalhost(HttpContext.Current.Request);
}
private void EnableCrossOriginRequestsFromLocalhost(HttpRequest request)
{
if (!HttpContext.Current.Request.IsLocal) return;
if (request.UrlReferrer == null) return; //can't set Access-Control-Allow-Origin header reliably without a referrer so just return. Referrer should always be set when being called from an app under development because the app under development's URL will be sent as the referrer automatically.
var response = HttpContext.Current.Response;
response.AddHeader("Access-Control-Allow-Origin", request.UrlReferrer.GetLeftPart(UriPartial.Authority));
response.AddHeader("Access-Control-Allow-Credentials", "true");
if (request.HttpMethod == "OPTIONS")
{
response.AddHeader("Access-Control-Allow-Methods", "POST, PUT, DELETE");
response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
response.AddHeader("Access-Control-Max-Age", "1728000");
response.End();
}
}
あなたのIIS上でCORSを許可する必要があります。 'preflight'はHTTP OPTIONS要求で、サーバは' Allow-Control- * 'ヘッダで応答する必要があります。ここでCORSの素晴らしい紹介です:http://www.html5rocks.com/en/tutorials/cors/ – zeronone
@zeronone私は、私の編集を見て、それdoesntの仕事を見て、それは取得要求に取り組んだが、投稿ではない – omriman12
このリンクを確認してください:http://stackoverflow.com/questions/33645511/why-my-ajax-showing-preflight-is-invalid-redirect-error – zeronone