如何传递客户端数据来表示哪些调用远程URL并获得响应

问题描述 投票:3回答:1

如何在http.get()中传递$ scope对象来表达和获取数据。这是我的代码,任何人都可以告诉我它有什么问题吗?所以端点网址是http://myhost:8080/employees/123。在这里,我需要从我的控制器动态传递empID到express服务器。我的代码无需传递empid即可运行并获取完整列表。

angularJs控制器

$scope.eid = "123";
$http.get('/employees/:empId=' + $scope.eid)
        .success(function (data, status) {
            $scope.employeeInfo = data;
        }).error(function (data, status) {
    });

服务器端代码

app.get("/employees/:empId", function(req,res) {
        
        var ServerOptions = {
            host: 'myHost',
            port: 8000,
            path: '/employees/:empId',
            method: 'GET'
        };
        var Request = http.request(ServerOptions,       function (Response) {
            ...
        });

    });
angularjs node.js express
1个回答
1
投票

在角度代码中你应该:

$scope.eid = "123";
$http.get('/employees/' + $scope.eid)
        .success(function (data, status) {
            $scope.employeeInfo = data;
        }).error(function (data, status) {
    });

并在快递

 app.get("/employees/:empId", function(req,res) {

    var ServerOptions = {
        host: 'myHost',
        port: 8000,
        path: '/employees/' + req.params.empId,
        method: 'GET'
    };
    var Request = http.request(ServerOptions,       function (Response) {
        ...
    });

});
© www.soinside.com 2019 - 2024. All rights reserved.