개발일기

Access-Control-Allow-Methods , CORS 문제시

알롱지 2020. 4. 22. 14:32
반응형

CORS ... 크로스도메인 문제시 

 

 

닷넷- 자바까지 가는건 확인했는데 다시 자바에서 닷넷으로 보내질 못했다. 

 

 

 

JSONP도 써보고 했지만 다양한 에러들 뜨고...;; 얘는 쌍방으로 POST 로 보내게고 받겠다 해도

 

type GET으로 나와서 집어치움...

 

닷넷에 Access-Control-Allow-Methods 추가하고 

 

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();
    EnableCrossDmainAjaxCall();
}

private void EnableCrossDmainAjaxCall()
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
        HttpContext.Current.Response.End();
    }
}

 

 

 

문제는 보내는 스크립트 쪽이였는데 

 

                    $.ajax({
                        type: 'POST',

                        url: '도메인주소'

                        cache: false, 
                        data: {'이름1' : 보낼데이터1, '이름2' : 보낼데이터2},

                        traditional: true, 


                        xhrFields: {
                            withCredentials: true
                        },

 

  success: function (data) {

 

  },

 error: function (error) {

 

 }

 

 

 

 

 

 

 

 

 

                        xhrFields: { 
                            withCredentials: true 
                        }, 

 

이거 세줄 추가 했다고 바로 해결 .... 내 일주일 ㅠ

 

 

반응형