2016-12-31 1 views
0

が、私はこのエラーを取得するのXMLHttpRequestは、httpをロードすることはできません:// localhostを:57997 /ホーム/私はMVCからWEBAPIにアクセスしようとすると

XMLHttpRequest cannot load http://localhost:57997/Home/Get. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:64035' is therefore not allowed acces 

Service.Jsゲット

app.service('MyService', function ($http) { 

    var ApiAddress = "http://localhost:57997/"; 

    this.GetWebData = function() { 
    var Getdatas= $http({ 
     url: ApiAddress + 'Home/Get', 
     type: 'GET', 
     dataType: 'json', 
     params: JSON.stringify(), 
     content:{'content-type' :'application/Json'} 
    }) 
    return Getdatas; 
    } 
}); 

Controller.Js

app.controller('WebCtrls', function ($scope,MyService) { 
    $scope.Hello = "Hello angular How r u..."; 

    $scope.GetDb = function() { 
    alert('Hello..'); 
    var SerData = MyService.GetWebData(); 
    SerData.then(function (d) { 
     $scope.Emp = d.data; 
    }) 
    } 
}) 

WEBAPI

public JsonResult Get() 
{ 
    var x = prod.GetEmployees(); 
    return new JsonResult { Data = x, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; 
} 

Global.asaxの

WEBAPIグローバルファイルで、私はあなたがChromeブラウザのWebセキュリティオプションを無効にすることで、それを扱うことができ、クロスページ起源

protected void Application_BeginRequest() 
{ 
    string[] allowedOrigin = new string[] { "http://localhost:57997/" }; 
    var origin = HttpContext.Current.Request.Headers["Origin"]; 
    if (origin != null && allowedOrigin.Contains(origin)) 
    { 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin); 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST"); 
     //Need to add more later , will see when required 
    } 
} 
+0

エラーメッセージには、原点が「http:// localhost:64035」であることが明確に記載されています。リソースは 'http:// localhost:57997 /'にあります。ポート64035はポート57997へのクロスオリジンアクセスを試みています。また、 'OPTIONS'は許可された方法でなければなりません。 – georgeawg

+0

sooこの解決策を克服する方法 –

答えて

0

ために怒鳴るのコードを書きましたchrome.exeが存在するフォルダの場所から次のコマンドを実行します。まずクロムのすべてのインスタンスを閉じます。そして、このような応答にヘッダを追加し、サーバーに来て、すべての要求をフィルタリングしながら、あなたは、サーバー側でそれを扱うことができ、次のコマンド

chrome.exe --disable-ウェブセキュリティ

を実行します。

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS"); 
+0

私はすでにWeApiファイルに同じ結果を追加しました。 –

+1

この行 "string [] allowedOrigin = new string [] {" http:// localhost:57997/"};" はこの文字列のようにする必要があります[] allowedOrigin = new string [] {"http:// localhost:64035 /"}; –

関連する問題