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

时间:2022-03-22 10:21:08

I want to get an addition(operation a+b = c) using JavaRest, method GET with 2 double parameters and AngularJS to get the generated resource, but I still blocked.

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

here is the code:

这是代码:

//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 test on server using http:

//使用http在服务器上进行成功测试:

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

and generated object is, a+b=c (5+5=10)

生成的对象是,a + b = c(5 + 5 = 10)

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

and here the PROBLEM: the AngularJS Controller with GET + 2 Params:

这里的问题:带有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 with ng-model:

使用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 个解决方案

#1


0  

Where u called this function? You have to pass this object into that function or simple write $scope.operation.a/b

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

#2


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

#1


0  

Where u called this function? You have to pass this object into that function or simple write $scope.operation.a/b

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

#2


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