【问题标题】:Redirect to another page after Ajax call?Ajax 调用后重定向到另一个页面?
【发布时间】:2019-01-24 20:59:53
【问题描述】:

所以这对我来说很难解释。我有一个剃须刀页面,当单击一个按钮时,它会调用一个 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 });
}

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

【问题讨论】:

  • 有没有尝试返回id,然后从ajax请求重定向到成功状态?
  • @joseatchang 我没有尝试过,我不确定该怎么做。你有什么我可以看的例子吗?
  • 查看第一个答案,他指的是对象传递给ajax方法,一旦获得对请求的肯定响应就会触发成功。
  • 普通的 html 表单可能比 ajax 更好,因为您不会尝试在不重新加载页面的情况下发送/检索数据。

标签: javascript ajax asp.net-mvc razor-pages


【解决方案1】:

我认为这就是你想要的,我在一个 MVC 5 项目中做了类似的事情,但我还没有在 Razor Pages 中对其进行测试:

这将是您的方法,请注意您应该将控制器添加到 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 的视图名称和参数

【讨论】:

  • 这个答案帮助我得到了我需要的东西。由于某种原因,我无法让 url.action 为我工作,但我一直在调整它,直到我得到下面的答案才能工作: return new JsonResult(new { path = "Account_Information?=" + offender.Account_ID.ToString()} );对于我的 ajax,我使用了 window.location.replace,如您的回答中所述,效果很好。
【解决方案2】:

重定向到页面返回 301 响应,格式如下:

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

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

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

【讨论】:

  • 如何在 PHP 中重定向到另一个页面后继续运行 ajax 调用?我的 ajax 调用正在执行一项非常复杂的任务,并且需要很长时间所以,是否可以在页面重定向后运行 ajax 调用?我在提交按钮单击时进行 ajax 调用,然后该页面重定向到另一个页面,然后我的 ajax 调用开始。
猜你喜欢
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2017-06-08
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多