与jQuery发布的数据,并重定向到另一个动作 [英] post data with jQuery and redirect to another action

查看:114
本文介绍了与jQuery发布的数据,并重定向到另一个动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看:

<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>

控制器动作:

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

        //some code

        return Json(new { redirect = Url.Action("Create") });
    }

TNX,这code解决我的问题。

Tnx, this code solve my problem

推荐答案

而不是返回重定向,这AJAX请求无法处理的,返回的URL为JSON,让处理后改变位置。

Instead of returning a redirect, which the AJAX request can't handle, return the URL as JSON and let the post handler change the location.

$.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" ) } );
}

这篇关于与jQuery发布的数据,并重定向到另一个动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆