简体   繁体   English

为什么我的AJAX帖子不会重定向到正确的ActionResult

[英]Why won't my AJAX post redirect to the correct ActionResult

So pretty simple set-up and I know I'm missing something stupid here. 这么简单的设置,我知道我在这里错过了一些愚蠢的东西。

JS: JS:

$("#Affiliate").change(function () {
                var selectedItem = $(this).val();
                $.ajax({
                    cache: false,
                    type: "POST",
                    url: "@(Url.Action("ChangeOrg", "ScheduleStatistic"))",
                    data: { "organazation": selectedItem }
                });
            });

View: 视图:

<select id="Affiliate" class="form_select" style="z-index: 3000; width: 100%;">
                                    <option value="Org1">Foo</option>
                                    <option value="Org2">Bar</option>

Controller: 控制器:

 public ActionResult ChangeOrg(string organazation)
    {
        switch (organazation)
        {
            case "Org1":
                return RedirectToAction("Index", new { orgURL = "URL_Org1" });
                break;
            case "Org2":
                return RedirectToAction("Index", new { orgURL = "URL_Org2" });
                break;

            default:
                return this.View();
        }

    }

However, while it gets to that action result... it never does the redirectToAction however if i type in the URL "Localhost/ChangeOrg?Organazation=Org1" It goes there correctly. 然而,当它到达那个动作结果时...它永远不会执行redirectToAction但是如果我输入URL“Localhost / ChangeOrg?Organazation = Org1”它正确地去了那里。 Any thoughts? 有什么想法吗?

Server-side redirect is actually done through client-side trip. 服务器端重定向实际上是通过客户端访问完成的。 Redirects are not relevant to ajax requests, so the browser never calls the redirected url. 重定向与ajax请求无关,因此浏览器从不调用重定向的URL。

What is the supposed outcome of Index action and how are you going to use it on succes of ajax? Index行为的假设结果是什么?如何在ajax的succes中使用它?

Depending on what you really need, this may work for you: 根据您的真实需要,这可能对您有用:

public ActionResult ChangeOrg(string organazation)
{
    switch (organazation)
    {
        case "Org1":
            return Index("URL_Org1");
            break;
        case "Org2":
            return Index("URL_Org2");
            break;

        default:
            return this.View();
    }

}

Update: 更新:

If the user should be redirected to a different page when Affiliate value changes, then you do not need ajax : 如果在Affiliate值更改时应将用户重定向到其他页面,则您不需要ajax

$("#Affiliate").change(function () {
  var selectedItem = $(this).val();
  var url = "@(Url.Action("ChangeOrg", "ScheduleStatistic"))?organazation=" + selectedItem,
  window.location.assign(url);
});

ajax won't redirect. ajax不会重定向。 To redirect you need to put it in success on the ajax call 要重定向,您需要在ajax调用中成功

$.ajax({
    cache: false,
    type: "POST",
    url: "@(Url.Action("ChangeOrg", "ScheduleStatistic"))",
    data: { "organazation": selectedItem }
    success: function(result){
        if(result.Success){
            window.location = "@Url.Action(newaction, newcontroller)"
        }
    }
});

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

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