使用$ http的2个参数的AngularJS GET请求

时间:2016-09-20 18:14:33

标签: javascript html angularjs json

我想使用JavaRest添加(操作a + b = c),使用2个双参数的方法GET和AngularJS获取生成的资源,但我仍然阻止。

这是代码:

//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();
}

//使用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);
        });
    };

});

使用ng-model的HTML:

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


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

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

    </tr>

2 个答案:

答案 0 :(得分:0)

你在哪里调用这个函数?  您必须将此对象传递给该函数或简单地写入$ scope.operation.a / b

答案 1 :(得分: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);
    });
};
});