2016-11-01 2 views
0

YDN-DB라는 프레임 워크에서 WebApi 서비스에 액세스하려고합니다. 로컬 저장소 유형 데이터베이스입니다. URL에서로드 할 수 있으며 크로스 원점 호출을 사용하여이를 수행하려고합니다. 내가 오류가 계속 나는 이유를 알아낼 수 없습니다WebAPI2 - Cross Origin 스크립팅 문제

var xhr = new XMLHttpRequest(); 
xhr.open('GET', url, true); 
var me = this; 
xhr.onload = function(e) { 
    var lines = xhr.responseText.split('\n'); 
    var documents = []; 
    for (var i = 0; i < lines.length; i++) { 
     var data = lines[i].split(';'); 
     if (data.length == 2) { 
      for(i=0; i<data.length; i++) { 
       // Here, I will be loading the records into the YDN-DB. 
      } 
     } 
    } 
    var msg = Documents.length + ' Documents loaded, indexing...'; 
    me.setStatus(msg); 
    var i=0; 
    var load = function(i) { 
     var n = 50; 
     me.db.put('Document', docs.slice(i, i + n)).then(function(keys) { 
     i = i + n; 
     if (i < docs.length) { 
      this.setStatus(msg + ' ' + i); 
      load(i); 
     } else { 
      this.setStatus(msg + ' done.'); 
     } 
     }, function(e) { 
     throw e; 
     }, me); 
    }; 
}; 
xhr.send(); 

:

는 XMLHttpRequest의를 생성하고 호출을 만드는 코드입니다.
XMLHttpRequest cannot load http://localhost:51946/api/Archive/c4020343622d57162cbdc11e603a49d93d64018e5c/1026697/1/10000/D. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8040' is therefore not allowed access. 

내 컨트롤러에서 내 WebAPI 기능, 나는 CORS가 활성화되고있다되고 원점에서 :

[HttpGet] 
[ResponseType(typeof(List<string>))] 
[Route("api/Archive/{LogonTicket}/{PID}/{MinRow}/{MaxRow}/{Direction}")] 
[EnableCors("http://localhost:8040", // Origin 
      "Accept, Origin, Content-Type, Options",        // Request headers 
      "GET",                 // HTTP methods 
      PreflightMaxAge = 600             // Preflight cache duration 
)] 
public object Archive(string LogonTicket, int PID, int MinRow, int MaxRow, string Direction) 
{ 
    if (svc.ValidateTicket(LogonTicket, PID)) 
    { 
     List<string> e = mp.GetArchiveJson(PID.ToString(), MinRow, MaxRow, Direction); 
     return e; 
    } 
    else 
    { 
     throw new AuthenticationException("Invalid LogonTicket"); 
    } 
} 

어떤 아이디어가? XMLHttpRequest에 두 번째 호출을 추가해야합니까?

+0

아마'Microsoft.AspNet.WebApi.Cors' NuGet 패키지를 설치했을 것입니다. 시동시 문서에 따라'HttpConfiguration.EnableCors()'도 실행 했습니까? https://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api#enable-cors – Snixtor

+0

내가 그 일을 잊어 버린 것 같아서 내가 확인해 보겠다. . – MB34

+0

알았어, 그걸 게시하면 받아 들일거야. 이런, 나는 그것을 잊었다는 것을 믿을 수 없다. – MB34

답변

0

"CORS 사용"에 대해서는 the documentation의 모든 단계를 따르십시오.

  1. 는 NuGet 패키지를 설치에 필요한 3 가지가있다. Install-Package Microsoft.AspNet.WebApi.Cors
  2. HttpConfiguration에서 CORS를 활성화합니다. 일반적으로 이것은 정적 방법입니다 : Register(HttpConfiguration config), config.EnableCors()에 대한 호출이 포함됩니다.
  3. 컨트롤러 또는 메소드를 EnableCors 속성으로 장식하십시오 (예 : EnableCors 속성의 귀하의 예를 사용 감안할 때 [EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]

, 그것은 당신이 전제 조건으로 # 1을 가지고 # 3를 가지고 분명하다, 그래서 당신은 또한 있어요. 당신이 2 번을 놓친 것처럼 보입니다.

관련 문제