简体   繁体   English

jquery ajax中的Javascript重定向

[英]Javascript redirect in jquery ajax

This is a continuation from my question last night.这是我昨晚问题的延续。 I have a JQuery AJAX response, as below.我有一个 JQuery AJAX 响应,如下所示。 I just need this to redirect to another view.我只需要这个来重定向到另一个视图。 See my code:看我的代码:

      $.ajax({
            url: "/Home/PersistSelections",
            type: 'post',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify(selectedItems),


            done: function (response) {
                window.location.href = "Home/GoToBooking";
            }
        })

The code in done, does not seem to work.完成的代码似乎不起作用。 I have also tried success.我也试过成功。 I'm also not sure what the parameter should be (response, or something else?).我也不确定参数应该是什么(响应,还是其他什么?)。

In addition, I have seen code like this:另外,我见过这样的代码:

return JavaScript("window.location = 'http://www.google.co.uk'");'

I have JavascriptResult resolving in my controllers, but when I try to use return Javascript , I can't seem to resolve the references?我的控制器中有JavascriptResult解析,但是当我尝试使用return Javascript ,我似乎无法解析引用? I am using ASP.NET Core 2.2.我正在使用 ASP.NET Core 2.2。

Try replacing -尝试更换 -

    done: function (response) {
        window.location.href = "Home/GoToBooking";
    }

With -和 -

        success: function (response) {
            window.location.href = "/Home/GoToBooking";
        }

In controller instead of JavascriptResult Use IActionResult在控制器而不是 JavascriptResult 中使用 IActionResult

In Js Use like this在 Js 中这样使用

$.ajax({
url: '//',
type: 'POST',
async: false,
data: { Your data },
success: function (data) {
    window.location.href = "/Home/Index";
 }
});

First,you need to be sure that the return data type is json because you set dataType:"json" in ajax.首先,你需要确定返回的数据类型是json,因为你在ajax中设置了dataType:"json"

Then done method should be used like below:然后应使用done方法,如下所示:

$.ajax({
        url: "/Home/PersistSelections",
        /....
     })
         .done(function (response) {
            window.location.href = "/Home/GoToBooking";
     })

Referece: jQuery.ajax handling continue responses: "success:" vs ".done"?参考: jQuery.ajax 处理继续响应:“成功:”与“.done”?

Finally,the href should be /Home/GoToBooking .最后,href 应该是/Home/GoToBooking

Here is a working sample you could refer to:这是您可以参考的工作示例:

1.View: 1.查看:

<button onclick="test()">click</button>

@section Scripts
{
<script>
    function test() {
        var selectedItems = { Id: 1, Namea: "aaa" };
         $.ajax({
            url: "/Home/PersistSelections",
            type: 'post',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify(selectedItems)
         })
             .done(function (response) {
                window.location.href = "/Home/GoToBooking";
         })
    }
</script>
}

2.Controller: 2.控制器:

[HttpPost]
public JsonResult PersistSelections([FromBody]Test test)
{
    return new JsonResult(test);
}

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

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