使用jQuery发布数据并重定向到另一个操作

Ogn*_*jen 2 asp.net-mvc

视图:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Index</h2>

<script type="text/javascript">


$(document).ready(function() {

   $(document).ready(function() {

    $("#example").submit(function() {
        var id = $("#id").val();
        var prezime = $("#prezime").val();

        $.post("/jQueryPost/PostingData", { id: id, prezime: prezime }, function(res) {
            if (res.redirect) {
                window.location.href = res.redirect;
                return;
            }

        }, "json");
        return false;
    });
});
</script>

<form id="example" method = "post">

    Id:<input id="id" type="text" />
    Prezime:<input id="prezime" type="text" />

<p>
    <input type="submit" value="Post Data" />
</p>
</form>

</asp:Content>
Run Code Online (Sandbox Code Playgroud)

控制器动作:

    [HttpPost]
    public ActionResult PostingData(int id, string prezime)
    {

        //some code

        return Json(new { redirect = Url.Action("Create") });
    }
Run Code Online (Sandbox Code Playgroud)

Tnx,这段代码解决了我的问题

tva*_*son 11

而不是返回AJAX请求无法处理的重定向,而是将URL作为JSON返回,让post处理程序更改位置.

$.post("/jQueryPost/PostingData", { id: id, prezime: prezime }, function(data) {
    location = data.url;
});

[HttpPost]
public ActionResult PostingData(int id, string prezime)
{

    //some code

    return Json( new { url = Url.Action( "Create" ) } );
}
Run Code Online (Sandbox Code Playgroud)