2012-09-06 101 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"; 
        });