2015-04-24 87 views
1

我是AngularJS的新手。PUT/POST在Java中使用AngularJS嘗試Rest api調用不起作用

我在嘗試從AngularJS調用PUT restful webservices時出現錯誤。這是我的工廠方法。

.factory('EmployeeService',function($q,$http){ 
    Approval:function(userId,firstName){ 
    var deferred = $q.defer(); 
    $http({ 
    method: 'PUT', 
    url: '/AngularExample/user', 
    params:{ 
    lastName:userId, 
    firstName:firstName 
    } 
    }). 
    success(function (data) { 

    if (data != null || data != 'undefined') { 

    deferred.resolve(data); 


    } 
    }) 
    .error(function (error) { 

    console.log('error'); 
    }); 
    return deferred.promise; 

    }) 

的EmployeeService是我的服務名稱

$scope.showAlert = function() { 
    EmployeeService.Approval($stateParams.userId,$stateParams.firstName).then(function (approval) { 
    $scope.approve= approval; 
    }) 

這是獲得同時呼籲在Java中把REST API調用的錯誤。

Angular JS version using :1.3.15 . 
Rest api call is not triggering from angular to Rest Back end and not getting any error in back End. 
Error getting in the angular js : 
     unexpected Request PUT No more request expected 
          at $httpBackend (angular-mocks.js:1180) 
          at m (angular.js:9628) 
          at $get.f (angular.js:9344) 
          at angular.js:13189 
          at l.$get.l.$eval (angular.js:14401) 
          at l.$get.l.$digest (angular.js:14217) 
          at l.$get.l.$apply (angular.js:14506) 
          at HTMLButtonElement.<anonymous> (ionic-angular.js:9958) 
          at HTMLButtonElement.c (angular.js:3014) 
          at triggerMouseEvent (ionic.js:2804) 

它在郵遞員工作。

+0

Web服務是工作在郵遞員其餘客戶端鉻 –

+0

您是否嘗試添加服務的完整URL,而不是/ AngularExample /用戶? – anbuj

+0

你是否在你的HTML中包含angular-mocks?不要那樣做 – thomaux

回答

0
i am using service and simple call the our REST API 
//in app/js 
var serverUrl = 'http://xxxxx.in/api'; 
.service('UserBankDetails', ['$http', 
     function($http) { 
      this.userBankDetailsAdd = function(data, cb) { 
       var res = $http.put(serverUrl + "/user_bank_account", data); 
       res.success(function(data, status, headers, config) { 
        cb(data); 
       }); 
      } 
     } 
    ]) 
    //simple call from controller 
UserBankDetails.userBankDetailsAdd($scope.userBankInfo, function(res) { 
    console.log(res); 
    if (res.data === 1) { 
     $scope.account_found = true; 
    } else { 
     $scope.account_found = false; 
    } 
});