代码之家  ›  专栏  ›  技术社区  ›  SamuraiJack

无法重定向到MVC控制器中的外部URL

  •  1
  • SamuraiJack  · 技术社区  · 6 年前

    我正在尝试在我的MVC项目中集成atoms支付网关。即使在启用CORS之后,我也会遇到这个严重的错误。所以我想可能是返回url导致了这个问题。所以我写了一个简单的重定向来找出真正的原因 return Redirect("https://google.com"); 但我还是犯了同样的错误。我做错什么了?

      public ActionResult TransferFund()
         {    
            return Redirect("https://google.com");
         }
    
    Failed to load https://google.com/: Response to preflight request doesn't pass 
    access control check: No 'Access-Control-Allow-Origin' header is present on the requested 
    resource. Origin 'http://localhost:25850' is therefore not allowed 
    access. The response had HTTP status code 405.
    

    我已经跟随了 This 重定向

    编辑

    <script type="text/javascript">
        $(document).ready(function () {
    
    
                $('#btnatom').on('click', function () {
                    $.ajax({
                        type: 'POST',
                        url: "/AtomPayment/TransferFund",
                        data: {},
                        dataType: "text",
                        success: function (resultData) { alert("Save Complete") }
                    });
                });
            });
    
    
        </script>
        [HttpPost]
        public ActionResult TransferFund()
        {
    
            return new RedirectResult("http://google.com");
            string strURL, strClientCode, strClientCodeEncoded;
            ....
        }
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Yara    6 年前

    无法在“TransferFund”方法中进行重定向,因为使用了“POST”方法。从jquery开始。此处更正代码:

    <script type="text/javascript">
        $(document).ready(function () {
    
    
                $('#btnatom').on('click', function () {
                    $.ajax({
                        type: 'POST',
                        url: "/AtomPayment/TransferFund",
                        data: {},
                        dataType: "text",
                        success: function (resultData) { 
                            if(resultData == "Ok"){
                                alert("Save Complete");
                                window.location = "https://google.com/"
                            }
                        }
                    });
                });
            });
    </script>
    
    [HttpPost]
    public ActionResult TransferFund()
    {
        //do some transfer work
        return Content("Ok");
    }
    
        2
  •  0
  •   Barr J    6 年前

    让我们深入了解错误:

    加载失败 https://google.com/ :对飞行前请求的响应 未通过访问控制检查:没有“访问控制允许源” 请求的资源上存在头。起源 ' http://localhost:25850 因此不允许访问。这个 响应具有HTTP状态代码405。

    你在打一个 Cross Domain request 问题。

    出于安全原因,谷歌拒绝你访问它的服务器。不管你是不是 POST GET 请求, google无法识别你的域名并阻止你的跨域请求 .

    如果您使用jquery,它将工作,是的,因为您使用 window.location java脚本的属性。

    如果要重定向来自控制器代码的结果,则需要使用本地域的地址,该地址将接受请求,并且不会拒绝该请求。

    你可以在这里读到更多关于 Error 405 .

    意思是:

    管理员可以配置每个web服务器,以便 方法是允许的还是不允许的。例如,如果没有 网站上的互动内容,其唯一合乎逻辑的是 方法是不允许的,因为用户没有输入自己的方法的选项 数据并发送到服务器。否则,所提到的错误消息 上面会显示状态代码405,通知浏览器和 不允许使用该方法的用户。