2014-06-12 32 views
0

我對Angular很陌生,試圖對一個簡單的Rest服務調用一個簡單的「Hello World」webservice調用,當我點擊它時,我已經驗證返回「Hello World」。

我有3個警報的方法。我看到「In method」警報,然後看不到任何其他警報。我附加了提琴手,Web服務請求從未製作過。我必須在這裏忽略一些基本的東西,我會認爲....關於我可能錯過了什麼的任何想法?

Fiddler顯示Web服務調用成功,我可以看到來自Web服務的結果,但我使用Chrome的開發人員工具顯示,對服務的調用正在被Angular內部的某些東西取消。

在此先感謝您提供的任何幫助。

  (function() { 

var app = angular.module("loginApp", ['ngResource']) 
    .config(function ($httpProvider) { 
     // Enable CORS 
     $httpProvider.defaults.useXDomain = true; 
     delete $httpProvider.defaults.headers.common['X-Requested-With']; 
    }); 

app.controller('LoginController', function ($scope, $http) { 
    this.loginImage = "images/ActiveView_ContentMgm_140x40.png"; 

    this.loginUser = function() { 
     var token = ""; 
     var loginUrl = "http://ten1.com/services/rest/demoservice.svc/Login"; 

     delete $http.defaults.headers.common['X-Requested-With']; 
     var result = $http({ method: 'GET', url: loginUrl, params: { userName: this.userName, passWord: this.passWord } }) 
     .success(function (data, status) { 
      $scope.status = status; 
      $scope.data = data; 
     }) 
     .error(function (data, status) { 
      $scope.data = data || "Request failed"; 
      $scope.status = status; 
     }); 

     token = data.Token; 
    }; 

}); 

})(); 更新:

現在我點擊登錄表單上的提交按鈕,只是試圖找回一個字符串,所以我可以驗證與Web服務的基本通信。我的目標是傳遞用戶名/密碼並取回令牌。這裏的形式:

    <form ng-submit="loginCtrl.loginUser()" novalidate> 
         <div class="form-group"> 
          <label for="exampleInputEmail1">Username</label> 
          <input type="text" class="form-control" style="border-radius:0px" ng-model="loginCtrl.loginForm.userName" placeholder="Enter username"> 
         </div> 
         <div class="form-group"> 
          <label for="exampleInputPassword1">Password </label> 
          <input type="password" class="form-control" style="border-radius:0px" ng-model="loginCtrl.loginForm.passWord" placeholder="Password"> 
         </div> 
         <button type="submit" class="btn btn-sm btn-default">Log in</button> 
+0

你做跨域GET請求?也許看看這裏:http://stackoverflow.com/questions/16085700/angularjs-http-get-is-getting-cancelled – pixelbits

+0

我檢查了這篇文章,顯然是我使用的角(1.2.17)版本不再支持$ httpProvider ....調用仍然被角度的東西取消,但實際的Web服務請求仍在工作 –

回答

2

進樣$範圍和$ HTTP到控制器中,而不是在你的loginUser()功能:

app.controller('LoginController', function ($scope, $http) { 
+0

謝謝,這讓我成爲那裏的一部分。知道它必須是基本的東西。現在調用已經完成,但每次都是錯誤的,即使我手動打開了Web服務URL,它的功能仍然很好。試圖現在排除故障。有什麼想法嗎? –

+0

更新:現在調用了,但每次都是錯誤的,即使在小提琴手中,web服務調用是成功的,並且我得到了我期待的數據。那麼爲什麼它最終在「錯誤」部分,當fiddler顯示調用工作得很好? –

+0

不確定,console.log(數據)在錯誤處理程序中顯示的是什麼? – pixelbits