我正在尝试从位于我的域中的IIS服务器上的MVC控制器访问数据。我收到此错误
"No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:22205' is therefore not allowed access. "
这是我的ajax电话:
binDropDownDataSource: new kendo.data.DataSource({
autobind: false,
serverFiltering: true,
dataType: "json",
crossDomain: true,
transport: {
read: {
cache: false,
//url: "/LogisticsWebApp/Requisitions/GetBins", This works if unremarked
url: "https://www.MyDomain.com/LogisticsWebApp/requisitions/getsites",
xhrFields: {
withCredentials: true
},
data: function () {
{
siteCode: viewModel.site.SiteCode,
shopCode: viewModel.binShopBlock.ShopCode
};
}
}
}
})
这是我的控制器:
public JsonResult GetBins(string siteCode, string shopCode)
{
var lookups = new Lookups();
var data = lookups.GetBins(siteCode,shopCode);
return Json(data, JsonRequestBehavior.AllowGet);
}
我希望能够将应用程序用作我的数据层,但需要能够针对它进行开发。
答案 0 :(得分:0)
如果您需要使用此特定方法,则可以将GetBins
操作更改为:
public JsonResult GetBins(string siteCode, string shopCode)
{
HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
var lookups = new Lookups();
var data = lookups.GetBins(siteCode,shopCode);
return Json(data, JsonRequestBehavior.AllowGet);
}
修改强>
如果您需要从不在控制器中的方法执行此操作,则必须使用:
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
答案 1 :(得分:0)
添加标题(" Access-Control-Allow-Origin"," *")以及标题(" Access-Control-Allow-Headers"," X-Requested-With")
System.Web.HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
System.Web.HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "X-Requested-With");