2016-11-01 10 views
0

WebApiサービスにYDN-DBというフレームワークでアクセスしようとしています。ローカルストレージタイプのデータベースです。それはあなたがURLからロードすることを可能にし、 私はそれをCross Originコールを使ってやろうとしています。WebAPI2 - クロスオリドスクリプトの問題

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"); 
    } 
} 

任意のアイデア? 2番目の呼び出しを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のすべての手順を必ず実行してください。必要なものは3つあります:

  1. NuGetパッケージをインストールします。 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を見逃したように見えます。

関連する問題