从angular post方法获取数据

Get data from angular post method

本文关键字:获取 数据 方法 post angular      更新时间:2023-09-26

我有一个带有http.post请求的angular控制器,但我没有得到响应,该服务运行良好,因为使用poster进行了测试。

当页面加载时,我需要在HTLM表中显示来自WS的数据(我知道如何完成这一部分),我将以变量名"data"发送请求的正文,以变量名为"config的头配置,以及对WS- URL的http.post调用

我刚接触过angular,所以我不知道我是否遗漏了什么,我也想在控制台中打印响应,以测试它是否返回了我期望的结果。

我从网上找到的一个例子中获取了这段代码,并对其进行了修改,在这个例子中,有一个按钮可以调用SendData()函数,但我不需要按钮,必须在加载页面时进行调用。

这是控制器代码

.controller("HttpGetTestController", function ($scope, $http) {
    $scope.SendData = function () {
        var data = {
            "userId":"mdrodri",
            "token":"840430482834947828876768058086529370654",
            "domain":"MX",
            "filters":{
                "timeFrameType":"Week (FiscalYear)",
                "custMembSelectionType":"TOTAL",
                "locationSelectionType":"Country",
                "merchandiseSelectionType":"SBU",
                "startYear":"2015",
                "endYear":"2015",
                "startUnit":"1",
                "endUnit":"1",
                "comparator":false,
                "locationSelections":["CA"],
                "merchandiseSelections":["SBU38"],
                "custMembSelections":["TOTAL"],
                "metricSelections":["Product Sales (Category Rank)"],
                "rankOrder":"10"
            },
            "additionalFilters":[],
            "cache":false
        };
        var config = {
            headers : {
                'Content-Type': 'application/json;'
            }
        }
        $http.post('http://whateverurl/', data, config)
            .success(function (data, status, headers, config) {
                $scope.PostDataResponse = data;
                console.log("Success");
            })
            .error(function (data, status, header, config) {
                $scope.ResponseDetails = "Data: " + data +
                    console.log("Data: " + data);
                    console.log("Status: " + status);
                    console.log("Headers: " + header);
                    console.log("Config: " + config);
            });
    };
})

在我的HTML中,这就是我如何调用控制器

 <div class="panel panel-default" ng-controller="HttpGetTestController">

感谢您的时间和帮助。

您需要调用控制器中的$scope.SendData()函数:

      .controller("HttpGetTestController", function ($scope, $http) {
        $scope.SendData = function () {
           //your code ...
         }
       //then the calling 
       $scope.SendData();//here
}