が、私はこのエラーを取得するの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
}
}
エラーメッセージには、原点が「http:// localhost:64035」であることが明確に記載されています。リソースは 'http:// localhost:57997 /'にあります。ポート64035はポート57997へのクロスオリジンアクセスを試みています。また、 'OPTIONS'は許可された方法でなければなりません。 – georgeawg
sooこの解決策を克服する方法 –