0

I am trying to access data from an MVC controller that is on an IIS Server located within my domain. I'm getting this error

"No 'Access-Control-Allow-Origin' header is present on the requested resource.
 Origin 'http://localhost:22205' is therefore not allowed access. " 

Here is my ajax call:

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

Here is my controller:

public JsonResult GetBins(string siteCode, string shopCode)
{
    var lookups = new Lookups();
    var data = lookups.GetBins(siteCode,shopCode);
    return Json(data, JsonRequestBehavior.AllowGet);
}   

I want to be able to use an application as my data layer but need to be able to develop against it.

Groyoh
  • 342
  • 1
  • 7
Alan Fisher
  • 2,005
  • 4
  • 41
  • 61
  • http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work or http://stackoverflow.com/questions/19174435/how-to-add-access-control-allow-origin – CSharper Nov 10 '14 at 20:16
  • @alan This comment is unrelated to your issue but is your `data` property correct? I keep wondering what that function is supposed to do since it does not return anything... – Groyoh Nov 10 '14 at 21:19
  • @Groyoh, The data function is for passing the parameters. I haven't yet figured out the difference between that and the Parameter Map function, they both seem to work just fine. – Alan Fisher Nov 10 '14 at 21:55

2 Answers2

0

If you need it your for this particular method, you might change your GetBins action to:

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

Edit

If you need to do that from a method that is not in the controller you must use:

HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
Groyoh
  • 342
  • 1
  • 7
  • ,I tried this but am getting a red squiggly under "context" saying I'm missing an assembly or reference. Do you know which ones? – Alan Fisher Nov 10 '14 at 23:09
  • It seems that I made a mistake in my answer. I've edited it. If you still can't find `HttpContext`, try to import `System.Web.HttpContext` if you are in a class or `System.Web.HttpContextBase` if you are in a controller. If it still doesn't work, check that you properly reference the `System.Web.dll` assembly. – Groyoh Nov 11 '14 at 00:28
0

Add header ("Access-Control-Allow-Origin","*") as well as header ("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");
Abbas Galiyakotwala
  • 2,949
  • 4
  • 19
  • 34