简体   繁体   中英

Using Access-Control-Allow-Origin with MVC

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM