2016-09-20 63 views
0

我想要使用JavaRest,方法GET和2個雙參數和AngularJS來獲得生成的資源,但我仍然被阻塞,從而得到一個加法(操作a + b = c)。

這裏是代碼:

//CalculatorResource.java 
@Path("calco") 
public class CalculeteResource { 

double result; 

@GET 
@Path("/addition/{a}/{b}") 
@Produces(MediaType.APPLICATION_JSON) 
public Response getAddition(@PathParam("a") double a, @PathParam("b") double b) { 

    Calculete calco = new Calculete(); 
    calco.setA(a); 
    calco.setB(b); 
    result = calco.getA() + calco.getB(); 
    calco.setC(result); 
    return Response.ok(calco).build(); 
} 

// succesfull服務器上使用http測試:

http://localhost:8380/dadem-calculator/rest/calco/addition/5/5

和生成的對象是,A + B = C(5 + 5 = 10 )

{"a":5.0,"b":5.0,"c":10.0} 

這裏的問題:帶有GET + 2參數的AngularJS控制器:

var app = angular.module("CalculatorManagement", []); 

//Controller 
app.controller("CalculatorController", function($scope, $http) { 

    $scope.result; 
    $scope.operation={ 
      a : 0, 
      b : 0, 
      c : 0 
     }; 


    //HTTP GET- get all users collection 
    function _refreshUserData() { 
     $http({ 
      method : 'GET', 
      url : 'http://localhost:8380/dadem-calculator/rest/calco/addition/' + operation.a + '/' + operation.b 
     }).then(function successCallback(response) { 
      $scope.result = response.data; 
     }, function errorCallback(response) { 
      console.log(response.statusText); 
     }); 
    }; 

}); 

HTML與NG-型號:

 <td><input type="number" ng-model="operation.a"></td> 


     <td><input type="number" ng-model="operation.b"></td> 

     <td> {{ result.c }} </td> 

    </tr> 
+1

有一個在你的HTTP GET一個錯字:+ operation.a「/」,你缺少一個「+ 」。它應該是+ operation.a +'/' –

+0

你是否在控制檯中發現任何錯誤? – Aravind

+0

您在Developer Tools的網絡標籤中看到了什麼? – NicolasMoise

回答

0

其中u調用此函數? 你必須對這個對象傳遞到該函數或簡單的寫$ scope.operation.a/B

+0

我有嘗試,但它不工作 – Manu

+0

功能_refreshUserData($ scope.operation.a/B){ \t \t \t $ HTTP({ \t \t \t \t方法: 'GET', \t \t \t \t網址:' HTTP://本地主機:8380/dadem計算器/休息/ CALCO /添加/」 + operation.a + '/' operation.b \t \t \t})然後(函數successCallback(響應){ \t \t \t \t。 $ scope.result = response.dat一個; \t \t \t},功能errorCallback(響應){ \t \t \t \t的console.log(response.statusText); \t \t \t}); \t \t}; \t \t \t \t $ scope.operation.a = result.a; \t \t $ scope.operation。b = result.b; \t \t $ scope.operation.c = result.c; – Manu

0
var app = angular.module("CalculatorManagement", []); 
app.controller("CalculatorController", function($scope, $http) { 

$scope.result; 
$scope.operation={ 
     a : 0, 
     b : 0, 
     c : 0 
    }; 


//HTTP GET- get all users collection 
function _refreshUserData() { 
    $http({ 
     method : 'GET', 
     url : 'http://localhost:8380/dadem-calculator/rest/calco/addition/' + $scope.operation.a + '/' + $scope.operation.b 
    }).then(function successCallback(response) { 
     $scope.result = response.data; 
    }, function errorCallback(response) { 
     console.log(response.statusText); 
    }); 
}; 
});