简体   繁体   English

使用Access-Control-Allow-Origin和MVC

[英]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. 我正在尝试使用简单的HTML与JQuery页面和另一个域上的MVC站点进行简单的跨域调用。

I'm basing what I do on this ... 我的基础是我做的...

Setting Access-Control-Allow-Origin in ASP.Net MVC - simplest possible method 在ASP.Net MVC中设置Access-Control-Allow-Origin - 最简单的方法

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 . XMLHttpRequest无法加载http://example.com:20874/Home/YourMethod Origin http://example.com:90 is not allowed by Access-Control-Allow-Origin. Access-Control-Allow-Origin不允许使用Origin http://example.com:90

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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