1

I have an angular controller with an http.post request but I'm not getting response, the service works fine because a test it using postman.

I need to display the data from the WS in an HTLM table(I know how to do this part) when the page is loaded, I'm sendinng the body for the request in a variable name "data", the header configuration un a variable name "config" and the the http.post call to the URL of my WS.

I'm new to angular so I don't know if I'm missing something, also I want to print the response in the console to test if it's returnig what I'm expecting.

I took this code from an example I found on the web and I modify it, in the example there's a button where the call to SendData() function but I don't need a button, the call has to be made as the page is loaded.

This is the controller code

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

    };

})

In my HTML this is how I'm calling that controller

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

Thank's for the time and help.

6
  • Does http://whateverurl/ support CORS? If it doesn't you will not be able to call it from a different domain due to the same origin policy restriction that is built into browsers. Which explains why your request works when made from Postman but not using AJAX in a browser. Just look at the console of your browser and you will see the warning saying that the request has been blocked. Commented Dec 30, 2015 at 20:51
  • Server has to enable CORS headers, you have to add $httpProvider.defaults.useXDomain = true; Commented Dec 30, 2015 at 21:00
  • Actually I'm working on a local network and I check my console and there's nothing in there. :( Commented Dec 30, 2015 at 21:04
  • @AlbertCyberhulk And where shoul I add that $httpProvider thing? because I and my header type in the config variable. Commented Dec 30, 2015 at 21:06
  • Can you see the request being sent in the Network tab of your browser? Commented Dec 30, 2015 at 21:07

1 Answer 1

1

You need to call $scope.SendData() function in the controller :

      .controller("HttpGetTestController", function ($scope, $http) {
        $scope.SendData = function () {
           //your code ...
         }
       //then the calling 
       $scope.SendData();//here
}
Sign up to request clarification or add additional context in comments.

7 Comments

Hi @AhmedAbdelraouf I add that line but nothing happen... :(
did you include the controller js file in the html file?
Yes, actually I have another controller but that one is reading a json, I made this to test that angular was working good, then I create this controller to use the WS but nothing happen, I can realize that my code is right or not?
try to add debugger; or a breakpoint at the beginning of the controller and inside the function and open the developer tool from your browser and refresh your html page if it hits in the controller this mean the html knows that the div block belongs to that angular controller
it does not matter if its post or get just use your PostDataResponse so when the request succeed the object will be initialized and the the binding will work <tr ng-repeat="x in PostDataResponse "> <td>{{x.Name}}</td> <td>{{x.Country}}</td> </tr>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.