2013-08-06 141 views
0
$('#loginbtn').click(function() { 
      var userName = document.getElementById('uid').value; 
      var password = document.getElementById('pwd').value; 

      $.ajax({ 
       type: "POST", 
       url: "/LoginNew.aspx/Authenticate", 
       data: "{ 'userName': '" + userName + "' ,'password': '" + password + "' }", 
       async: false; 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: a(), 
       error: function(e) { 
        alert(e.valueOf()); 
       } 
      }); 

     alert("test"); 
      function a() { 
       window.location.href = "Login.aspx"; 
      } 
     }); 

因爲我接受了答案,它轉到服務器端代碼驗證用戶和控制傳遞給「a」函數,但它不顯示login.aspx頁面...任何線索?Ajax調用不起作用

+1

什麼不行,呼叫或重定向? – Spokey

+0

它永遠不會調用驗證方法。 –

回答

3

應該

$('#loginbtn').click(function() { 
    var userName = document.getElementById('uid').value; 
    var password = document.getElementById('pwd').value; 

    $.ajax({ 
     type : "POST", 
     url : "/LoginNew.aspx/Authenticate", 
     data : { 
      userName: userName , 
      password: password 
     }, 
     async : false, // not ; need to use , 
     contentType : "application/json; charset=utf-8", 
     dataType : "json", 
     success : a, // pass the callback reference, don't invoke it 
     error : function(e) { 
      alert(e.valueOf()); 
     } 
    }); 

    alert("test"); 
    function a() { 
     window.location.href = "Login.aspx"; 
    } 
}); 
+0

它的工作..謝謝很多。 –

+0

它不會切換到「Login.aspx」 –

+0

它不會切換到「Login.aspx」 –

2

您正在生成的JSON是無效的。不要嘗試手工生成JSON,請使用JSON庫。

JSON.stringify({ userName: userName, password: password }) 

調用a()而不是作爲成功處理程序分配a。如果要分配函數而不是返回值,請刪除()

+0

window.location.href =「Login.aspx」;不起作用,雖然控制功能「a」。 –