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?
Cross Origin Resource Sharing (CORS) is a W3C standard that allows a server to relax the same-origin policy. Using CORS, a server can explicitly allow some cross-origin requests while rejecting others. CORS is safer and more flexible than earlier techniques such as JSONP.
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""}");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With