내 서버 쪽은 C#
mvc 프로젝트입니다.dev 모드에서 반응하기
우리는 클라이언트 측에 대한 반응을 구현하려고합니다.
나는 express
서버와 hot-reloading
를 사용하여, npm
와 node-js
을 사용하고, 그래서 난 내 클라이언트 측 코드를 컴파일 할 때 그것은 http://localhost:3000에서 실행됩니다.
이제 서버 쪽 통화를 추가하고 싶습니다. 이렇게하려면 다른 포트의 localhost에서도 열리는 iis express를 사용하여 c#
코드를 실행하십시오. 이제 문제는 포트 : 3000의 클라이언트 코드가 localhost에있는 iis express로 아약스 호출을 할 때 동일한 도메인 정책으로 인해 "Response for preflight is invalid (redirect)"
오류가 발생한다는 것입니다.
그래서 내가 뭘 잘못하고있다, 당신은 어떻게 당신의 서버와 클라이언트가 분리되어있을 때 dev 모드에서 일한다고 생각합니까? 당신이 그렇게 먼저 클라이언트는 OPTIONS
요청을 보내고, 다른 도메인으로 게시물을 보낼 때 솔루션
-
는 내 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>
편집에 추가했습니다. 그래서 솔루션이 코드를 추가 할 수 실제로 : 귀하의 경우에는
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'는 서버가'Allow-Control- *'헤더로 응답해야하는 HTTP OPTIONS 요청입니다. CORS에 대한 훌륭한 소개는 다음과 같습니다. http://www.google.com/intl/ko/tutorials/cors/ – zeronone
@zeronone 제 편집을 보았습니다. 작업을 수행하지 못했지만 요청을 받았지만 게시 할 수 없었습니다. – omriman12
이 링크를 확인하십시오 : http://stackoverflow.com/questions/33645511/why-my-ajax-showing-preflight-is-invalid-redirect-error – zeronone