2016-09-23 29 views
0

我試圖從我的login.html,使用javascript,我在login.html將有一個來自MongoDB的驗證從我的account.html。如果它是相同的數據,它應該去acoount.html,但它不是。這是我的javascript:通過javascript移動到其他html

.controller('LoginCtrl', function($scope, $state, $ionicPopup, AuthService) { 
    $scope.data = {}; 

    $scope.login = function(data) { 
      var formdata = { 

        num : $("#num").val(), 
        pw : $("#pw").val() 
      }; 

      var Jformdata = JSON.stringify(formdata); 
      //var url = $location.url(); 
      console.log(Jformdata); 

      $.ajax({ 
       url : "/ProjectSinarmas/submit", 
       context : document.body, 
       type : 'POST', 
       data : Jformdata, 
       contentType : "application/json" 
      }).done(function (response){ 
       //console.log(response); 
       if(response == "true"){ 
        //$state.go('main.account'); 
        var path = $location.path("http://localhost:8089/ProjectSinarmas/templates/account.html"); 
        alert("Login Success"); 
       }else{ 
        $scope.showAlert('Nomer Telephone dan PIN Salah'); 
       } 

      }); 


     }; 

我從我的html這個錯誤響應:

controllers.js:53 Uncaught ReferenceError: $location is not defined 

感謝之前,並有一個愉快的一天。

+0

你能顯示響應中的內容嗎? – snit80

+0

@ snit80 controllers.js:53 Uncaught ReferenceError:$ location is not defined –

回答

0

你需要調用$位置控制器檢查以下

.controller('LoginCtrl', function($scope, $state,$location, $ionicPopup, AuthService) { 


//code........ 

} 
+0

它不顯示任何錯誤,但它不會移動到我的帳戶,html –

+0

但如果您需要使用「$ location.path」,你需要像上面 –

+0

那樣定義它的工作原理,謝謝! –

0

除了增加$位置控制器,我的猜測是,你可能還需要使用AngularJS $http method(或$ POST)方法,而不是的$ ajax,以便AngularJS知道您的請求何時完成並處理範圍更新。

也隨之變化:

var path = $location.path("http://localhost:8089/ProjectSinarmas/templates/account.html"); 

到:

$location.path("http://localhost:8089/ProjectSinarmas/templates/account.html"); 

沒有必要把路徑VAR如果不使用。

+0

我試了一下這樣的,它簡化版,工作:$ HTTP({ \t \t \t \t方法: 'GET', \t \t \t \t網址:「HTTP://本地主機:8089/ProjectSinarmas /模板/帳戶。html的」 \t \t \t \t})然後(函數successCallback(響應){ \t \t \t \t \t $ location.path( 「HTTP://本地主機:8089/ProjectSinarmas /模板/ account.html」); \t \t \t \t \t alert(「Login Success」); \t \t \t \t},功能errorCallback(響應){ \t \t \t \t \t $ scope.showAlert( 'Nomer電話擔PIN薩拉赫'); \t \t \t \t}); –

+0

錯誤是什麼?你在這裏發佈的代碼在url後面有一個分號,而你不需要命名成功和錯誤函數,如下所示:'$ http({method:'GET',url:'localhost:8089/ProjectSinarmas/templates/(函數(響應)){location.path(「localhost:8089/ProjectSinarmas/template s/account.html」); alert(「Login Success」); }, 。功能(響應){範圍。showAlert('Nomer Telephone and PIN Salah'); });' – laney

+0

實際上沒有錯誤 –