1

我正在使用 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 来发布数据以访问我的数据库,在 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

        }
    }

脚本:

$("#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);
        }
    });
});
4

1 回答 1

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";
                   });
于 2012-09-06T19:35:12.597 回答