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

时间:2018-08-07 09:41:29

标签: c# jquery ajax asp.net-mvc cors

我正在尝试将原子支付网关集成到我的MVC项目中。即使启用了CORS,我也一直收到这个讨厌的错误。因此,我认为可能是导致问题的返回网址。因此,我编写了一个简单的重定向来找出真正的原因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;
        ....
    }

3 个答案:

答案 0 :(得分:1)

由于使用“ POST”方法,因此无法在“ TransferFund”方法中进行重定向。从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");
}

答案 1 :(得分:0)

让我们深入研究错误:

  

无法加载https://google.com/:对预检请求的响应   没有通过访问控制检查:否'Access-Control-Allow-Origin'   标头出现在请求的资源上。起源   因此,不允许访问“ http://localhost:25850”。的   响应的HTTP状态代码为405。

您遇到Cross Domain request问题。

出于安全考虑,Google拒绝了您访问服务器的请求。发出POSTGET请求都没关系, google不会识别您的域并阻止您的跨域请求

如果使用jquery,它将起作用,是的,因为您使用Java脚本的window.location属性从客户端到客户端发出了请求。

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

您可以在此处了解有关Error 405的更多信息。

的意思是:

  

管理员可以配置每个Web服务器,以便单个   方法是允许的还是不允许的。例如,如果没有   网站上的互动内容,因此POST   不允许使用此方法,因为用户无法选择输入自己的方法   数据并将其发送到服务器。否则,出现错误信息   上面出现状态代码405,通知浏览器和   它的用户不允许使用该方法。

答案 2 :(得分:0)

以防万一有人需要:

首先将你的另一个“http://example.com”重定向到另一个没有 httpMethod 的控制器(“RedirectController”)

[HttpPost]
public IActionResult TransferFund(){
   Return Redirect("redirect?url=http://example.com");
}

现在:

<块引用>

重定向控制器.cs

RedirectController(){
  public IActionResult Indext(){
     var url = Request.Query["url"];
     Return Redirect(url);
  }
}
<块引用>

或者我们可以重定向到当前控制器 btw