在单击事件中的ajax请求后重定向

时间:2012-09-06 19:19:58

标签: c# javascript jquery asp.net ajax

我正在使用JScript + ASP.NET。我有一个带有2个输入(用户和密码)和一个按钮的表单。我要做的是:

1- Fire a click event
2- Look inside a database if the user exist
3- Give back the answer
4- If the answer is true, POST some data to an other page AND redirect to it.

我首先尝试使用ASP.NET执行此操作。要使用ASP.NET发布数据,我需要使用PostBackUrl属性,但问题是PostBackUrl忽略了我的点击事件。

然后我尝试用jscript做这个。在我的点击事件(jquery)上,我使用$ .ajax来POST数据来访问我的数据库,在json中给出答案......我被困在那里。在这两种方法中,我都陷入了第4点。

ASP.NET

protected void SignIn_OnClick(object sender, EventArgs e)
    {
        Clients client = (Clients)clientDAO.getUsername(text1.Text, password2.Text);
        if (client != null)
        {
            Session.Add("SessionNoClient", "1272");
            Session.Add("CurrentQuote", "-1");
            Session.Add("UnitSystem", "0");
            Session.Add("SessionAdministrator", "0");

            //How to redirect with POST here

        }
    }

的JScript:

$("#m_bLogin").click(function () {
    var username = $("#text1").val();
    var password = $("#password2").val();
    var form = $("#formClient");
    $.ajax({
        url: '../../Class/LoginAjax.asmx/GetLoginInformation',
        data: "{ 'Name':'" + username + "','Password':'" + $("#password2").val() + "'}",
        dataType: "json",
        type: "POST",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            //My Json returns {"'Name':'Bob','Password':'1234'} and I'm not able to access Name or Password property. I tried data.d, data.d.Name, eval(data.d.Name) etc...
            form.submit();
        },
        error: function (XMLHttpRequest, textStatus, error) {
            alert(error);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

你可以这样做:

$.ajax({
    url: '../../Class/LoginAjax.asmx/GetLoginInformation',
    data: "{ 'Name':'" + username + "','Password':'" + $("#password2").val() + "'}",
    dataType: "json",
    type: "POST",
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        //My Json returns {"'Name':'Bob','Password':'1234'} and I'm not able to access Name or Password property. I tried data.d, data.d.Name, eval(data.d.Name) etc...
        form.submit();
    },
    error: function (XMLHttpRequest, textStatus, error) {
        alert(error);
    }
}).done(function() { 
                      window.location.href = "YourNewPage.aspx";
                   });