我想要使用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>
有一個在你的HTTP GET一個錯字:+ operation.a「/」,你缺少一個「+ 」。它應該是+ operation.a +'/' –
你是否在控制檯中發現任何錯誤? – Aravind
您在Developer Tools的網絡標籤中看到了什麼? – NicolasMoise