簡體   English   中英

角度上的http函數未定義

[英]http function undefined on angular

我是Angle和MEAN堆棧的新手,在此之前,我在這里提出了一個問題: Angular Routing ngRoute無法提取我的其他HTML文件

基本上,我沒有正確設置應用程序路由,解決方案是我必須將鏈接修改為#! 從#那里,有人說這是由於Angular 1.6的更改中斷造成的,在此之前我沒有一個可行的實現,但是它不是一個正確的一頁應用程序,因為它不能僅提取該應用程序的適當HTML。 好的,現在我可以查看和導航該應用了。

然后,當我嘗試與該應用進行通信時,當我單擊HTML頁面上的按鈕時,我遇到了另一個問題,該應用據說應該將我注冊到該應用中

<form class="form-auth" ng-submit="register()">
  <h2>Register</h2>
  <p class="text-warning">{{error_message}}</p>
  <input type="username" ng-model="user.username" placeholder="Username" class="form-control"><br>
  <input type="password" ng-model="user.password" placeholder="Password" class="form-control"><br>
  <input type="submit" value="Register" class="btn btn-primary" />

我在控制台日志上出現錯誤,說

Error: $http.post(...).success is not a function
at b.$scope.register (iotApp.js:73)
at fn (eval at compile (angular.js:15152), <anonymous>:4:144)
at e (angular.js:26673)
at b.$eval (angular.js:17958)
at b.$apply (angular.js:18058)
at HTMLFormElement.<anonymous> (angular.js:26678)
at bg (angular.js:3613)
at HTMLFormElement.d (angular.js:3601)(anonymous function) @ angular.js:14324(anonymous function) @ angular.js:10834$apply @ angular.js:18063(anonymous function) @ angular.js:26678bg @ angular.js:3613d @ angular.js:3601  

這是控制台指示的代碼部分,其中錯誤是

 app.controller('authController', function($scope, $http, $rootScope, $location){
   $scope.user = {username: '', password: ''};
   $scope.error_message = '';

   $scope.login = function(){
     $http.post('/auth/login', $scope.user).success(function(data){
       if(data.state == 'success'){
         $rootScope.authenticated = true;
         $rootScope.current_user = data.user.username;
         $location.path('/');
       }
       else{
         $scope.error_message = data.message;
       }
     });
   };

   $scope.register = function(){
     $http.post('/auth/signup', $scope.user).success(function(data){
       if(data.state == 'success'){
         $rootScope.authenticated = true;
         $rootScope.current_user = data.user.username;
         $location.path('/');
       }
       else{
         $scope.error_message = data.message;
       }
     });
   };
 });

我已經閱讀了其他一些答案,但是其中大多數答案已經不小心將依賴項放入了已導入控制器的方法參數中。 當我使用Advanced REST Client(這是一個Chrome擴展程序,用於手動發送注冊請求)時,它工作正常,但是當我使用該應用程序時,它不能正常工作。 任何幫助或只是一般的指針,將不勝感激。 謝謝!

$http.post文檔中:

// Simple GET request example:
$http({
  method: 'GET',
  url: '/someUrl'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
}, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM