繁体   English   中英

Ajax 调用后重定向到另一个页面?

[英]Redirect to another page after Ajax call?

所以这对我来说很难解释。 我有一个 razor 页面,当单击按钮时,它调用一个 javascript 函数,该函数对后端的处理程序进行 ajax 调用。 处理程序执行一些操作并获取我想传递给另一个页面的 id。 我试图在后端使用 RedirectToPage 函数,但屏幕从未打开。 它成功调用了处理程序,但是当处理程序返回时,什么也没有发生。 有没有办法做到这一点?

这是从被单击的按钮调用的 javascript/ajax 代码。

@section scripts{
<script>

// Get the account ID Data from the row selected and return that to the program.
function getIDData(el) {        

    var ID = $(el).closest('tr').children('td:first').text();
    var iddata = {
        'ID': ID
    }        

    console.log(iddata);
    return iddata;
}

// Submit the data to a function in the .cs portion of this razor page.
$('.copybtn').click(function () {
    var accountid = JSON.stringify(getIDData(this));
    $.ajax({
        url: '/Copy_Old_Account?handler=CopyData',
        beforeSend: function (xhr) {
                        xhr.setRequestHeader("XSRF-TOKEN",
                                $('input:hidden[name="__RequestVerificationToken"]').val());
                },
        type: 'POST',
        dataType: 'json',            
        data: { offenderid: offenderid },    
        success: function (result) { 

        }, 
    });
});

</script>
}

对于我从 ajax 调用中调用的代码背后的代码,如下所示:

public ActionResult OnPostCopyData (string accountid)
{
   // Do my other stuff here
   return RedirectToPage("Account_Information", new { id = account.Account_ID });
}

任何帮助将不胜感激,如果没有意义,我可以尝试解决任何问题。

我认为这就是你想要的,我在 MVC 5 项目中做了类似的事情,我还没有在 Razor Pages 中测试过它:

这将是您的方法,请注意,您应该将 Controller 添加到Url.Action ,我个人还没有尝试过将参数与 url 一起传递,但我认为它会正常工作

[HttpPost]
public ActionResult SubmitSomething()
{
    return Json(new { redirectUrl = Url.Action("Account_Information", "YOUR_CONTROLLER_NAME", new { id = account.Account_ID }) });
}

然后这将是您的 Ajax 请求,我更新了success部分

// Submit the data to a function in the .cs portion of this razor page.
$('.copybtn').click(function () {
    var accountid = JSON.stringify(getIDData(this));
    $.ajax({
        url: '/Copy_Old_Account?handler=CopyData',
        beforeSend: function (xhr) {
                        xhr.setRequestHeader("XSRF-TOKEN",
                                $('input:hidden[name="__RequestVerificationToken"]').val());
                },
        type: 'POST',
        dataType: 'json',            
        data: { offenderid: offenderid },    
        success: function (result) { 
            if (result.redirectUrl !== undefined) {
                window.location.replace(result.redirectUrl);
            } else {
                // No redirect found, do something else
            }
        }, 
    });
});

这没有经过测试,所以我只能希望它现在对你有用

编辑:更新 Url.Action 以使用 OP 的视图名称和参数

重定向到页面返回 301 响应,其格式为:

HTTP/1.1 301 Moved Permanently
Location: http://www.example.org/index.asp

要在 ajax 调用后重定向,您可以通过以下方式重定向到请求的 url:

success: function (result) { 
window.location = result.getResponseHeader('Location');
}

暂无
暂无

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

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