2014-03-29 16 views
0

我試圖重定向到另一個頁面,這取決於我在腳本的回調函數中收到的數據。jQuery如何重定向

jQuery("button.logInBtn").click(function(){  
    if(jQuery('#userName').val() == "" || jQuery('#password').val() == ""){ 
     alertify.error("Please enter User Name and Password"); 
    } 
    else{ 
     jQuery.post(jQuery("#loginForm").attr("action"), 
       jQuery("#loginForm :input").serializeArray(), 
       function(data){ 
         alertify.success(data); 
       });   
    } 
    jQuery("#loginForm").submit(function(){ 
     return false; 
    }); 

}); 

我該如何讓這個腳本來檢查我在回調函數中收到哪些數據???然後根據不同的數據,我收到

+0

通過編寫代碼,通常。這完全取決於您希望恢復的數據類型以及您希望重定向的方式。作爲一個起點,在JavaScript中使用'document.location.href = blah;'來重定向。 – Dave

+0

如果數據等於某些東西,我可以在回調函數中使用嗎? – user3288033

+0

ive曾嘗試過像這樣 – user3288033

回答

0

這就是你需要

jQuery.post(jQuery("#loginForm").attr("action"), 
      jQuery("#loginForm :input").serializeArray(), 
      function(data){ 
       if(data=="something"){ // replace with your condition 
        document.location.href = "http://example.net"; //redirect to example.net 
}); 
+0

嗨謝謝你的幫助,但仍然不工作,這就是我得到的。 'jQuery.post(jQuery的( 「#登錄表單」)ATTR( 「動作」), \t \t \t \t \t jQuery的( 「#登錄表單:輸入」。)serializeArray(), \t \t \t \t \t \t功能(數據){ \t \t \t \t \t \t \t如果(數據== 「匹配」){ \t \t \t了window.location = 「/main/index.php」; \t \t \t返回true; \t \t} \t \t否則{\t \t \t \t \t \t \t alertify.error(數據); \t \t} \t \t \t \t \t \t}); \t' – user3288033

+0

'data'包含什麼?我相信這是關於條件的。 –

0

你必須定義alertify.success(data),與data是什麼來自阿賈克斯回重定向。

可以再用window.location.href = 'http://example.com'

一個快速的方法重定向到定義alertify.data是:

alertify = function() { 
    return { 
    success: function(data) { 
     if (data.username) { //or any other condition 
     window.location.href = ... 
     }; 
    }; 
    }; 
};