2015-04-07 71 views
-1

我想發送登錄數據到網站的api,如果statusCode返回302,那麼我想重定向用戶到另一個網頁。但我的jQuery沒有執行並給我錯誤$ ajax沒有定義。下面是我的jQuery代碼。

$("#asd").click(function(){ 

       alert("sas"); 
       $ajax({ 
          url:'url.com', 
          type: 'POST', 

          data: ({ 

           Username: $('#inputUserName').val(), 
           password: $('#inputPassword').val(), 
           returnURL: 'url.com', 
          }), 
          success : function(data, textStatus,jqXHR) 
          { 
           window.location.href="myvideo.html" 
          }, 
          error :function(jqXHR, textStatus, errorThrown) 
          { 
           alert("Invalid username and password"); 
          } 

        }); 

       }); 

}); 
+2

使用像這樣'$ .ajax({'not $ ajax({ –

+1

使用'$ .ajax(...' –

+0

謝謝。 @ ShaunakD – 1binary0

回答

3

$.ajax({})$ajax

$.ajax({ 
     url:'url.com', 
     type: 'POST', 
     data: ({ 
      Username: $('#inputUserName').val(), 
      password: $('#inputPassword').val(), 
      returnURL: 'url.com', 
      }), 
      success : function(data, textStatus,jqXHR) 
      { 
      window.location.href="myvideo.html" 
      }, 
      error :function(jqXHR, textStatus, errorThrown) 
      { 
      alert("Invalid username and password"); 
      } 
}); 
+0

你能告訴我怎樣才能得到由api返回的statusCode?如果用戶名和密碼正確,它將返回302,否則不會。 – 1binary0

+0

檢查此鏈接,如果你從這裏得到任何幫助: http://stackoverflow.com/questions/5344145/how-to-get-response-status-code-from-jquery-ajax –