Reputation: 3262
I'm trying to get simple cross domain call working with a simple HTML with JQuery page and an MVC site on another domain.
I'm basing what I do on this ...
Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method
Here's the call in my simple site ...
<script type="text/javascript">
$(function () {
$.get("http://example.com:20874/Home/YourMethod", function (data) {
alert(data);
});
});
</script>
and heres my controller ... the attribute code is just pasted from other question ...
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[AllowCrossSiteJson]
public ActionResult YourMethod()
{
return Json(@"{""title"": ""example glossary""}");
}
}
But the calling site errors with ...
XMLHttpRequest cannot load http://example.com:20874/Home/YourMethod. Origin http://example.com:90 is not allowed by Access-Control-Allow-Origin.
Can anyone assist please?
Upvotes: 6
Views: 3457
Reputation: 3262
Gave up with the attributes and just did it like this ...
public ActionResult YourMethod()
{
HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*");
return Json(@"{""title"": ""example glossary""}");
}
Upvotes: 9