Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在$.ajax回调中重定向到操作?_Asp.net Mvc_Ajax_Jquery - Fatal编程技术网

Asp.net mvc 如何在$.ajax回调中重定向到操作?

Asp.net mvc 如何在$.ajax回调中重定向到操作?,asp.net-mvc,ajax,jquery,Asp.net Mvc,Ajax,Jquery,我使用$.ajax()每5秒轮询一个操作方法,如下所示: $.ajax({ type: 'GET', url: '/MyController/IsReady/1', dataType: 'json', success: function (xhr_data) { if (xhr_data.active == 'pending') { setTimeout(function () { ajaxRequest(); }, 5000);

我使用$.ajax()每5秒轮询一个操作方法,如下所示:

$.ajax({
    type: 'GET', url: '/MyController/IsReady/1',
    dataType: 'json', success: function (xhr_data) {
        if (xhr_data.active == 'pending') {
            setTimeout(function () { ajaxRequest(); }, 5000);                  
        }
    }
});
以及行动结果行动:

public ActionResult IsReady(int id)
{
    if(true)
    {
        return RedirectToAction("AnotherAction");
    }
    return Json("pending");
}

为了使用RedirectToAction,我必须将action返回类型更改为ActionResult(最初它是JsonResult,我返回的是
Json(new{active='active'};
),但它似乎无法从$.ajax()成功回调中重定向和呈现新视图。我需要重定向到“AnotherAction”在这个轮询ajax回发中。Firebug的响应是来自“AnotherAction”的视图,但它不呈现。

您需要使用ajax请求的结果,并使用该结果运行javascript手动更新window.location。例如:

// Your ajax callback:
function(result) {
    if (result.redirectUrl != null) {
        window.location = result.redirectUrl;
    }
}

其中“result”是jQuery的ajax方法在完成ajax请求后传递给您的参数。(要生成URL本身,请使用
UrlHelper.GenerateUrl
,它是一个基于操作/控制器等创建URL的MVC帮助程序。)

我知道这是一篇非常古老的文章,但在浏览了网页之后,这仍然是谷歌的首选答案,我最终使用了另一种解决方案。如果你想使用纯RedirectToAction,它也可以工作。RedirectToAction响应包含视图的完整标记

C#:

return RedirectToAction("Action", "Controller", new { myRouteValue = foo});
 $.ajax({
    type: "POST",
    url: "./PostController/PostAction",
    data: data,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    complete: function (result) {
        if (result.responseText) {
            $('body').html(result.responseText);
        }
    }
});
JS:

return RedirectToAction("Action", "Controller", new { myRouteValue = foo});
 $.ajax({
    type: "POST",
    url: "./PostController/PostAction",
    data: data,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    complete: function (result) {
        if (result.responseText) {
            $('body').html(result.responseText);
        }
    }
});
C#运作良好

我刚更改了JS,因为responseText不适合我:

$.ajax({
类型:“POST”,
网址:postrl,
contentType:false,
processData:false,
async:false,
数据:requestjson,
成功:功能(结果){
如果(结果){
$('body').html(结果);
}
},
错误:功能(xhr、状态、p3、p4){
var err=“Error”+“”+状态+“”+p3+“”+p4;
if(xhr.responseText&&xhr.responseText[0]==“{”)
err=JSON.parse(xhr.responseText).Message;
控制台日志(err);
}
});   

您可以在视图中使用
Html.RenderAction
帮助程序:

public ActionResult IsReady(int id)
{
    if(true)
    {
        ViewBag.Action = "AnotherAction";
        return PartialView("_AjaxRedirect");
    }
    return Json("pending");
}
在“_AjaxRedirect”部分视图中:

@{
    string action = ViewBag.ActionName;
    Html.RenderAction(action);
}
参考:

在彻底搜索后,在另一篇文章中发现了类似的内容。唯一的区别是它使用了window.location.replace。谢谢!如果您在请求中添加参数“dataType:'json',您将在结果中获得所需的responseText变量。