2012-09-06 76 views
1

我正在使用JScript + ASP.NET。我得到了一个带有2个输入(用户和密码)和一个按钮的表单。我试图做的是:在单击事件中发出ajax请求后重定向

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 

     } 
    } 

的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); 
     } 
    }); 
}); 

回答

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