0

I am trying to fetch a string from aspx backend to AngularJS using http post but it is returning undefined. Here is my code

<body data-ng-app="myapp">
<div data-ng-controller="MyController" >
    <button data-ng-click="myData.doClick(item, $event)">Send AJAX Request</button>
    <br/>
    Data from server: {{myData.fromServer}}
  </div>
<script>
      angular.module("myapp", [])
          .controller("MyController", function ($scope, $http) {
              $scope.myData = {};
              $scope.myData.doClick = function (item, event) {
              $http.post('KeyPhraseGroup.aspx/GetEmployees', { data: {} })
                .success(function (data, status, headers, config) {
                    $scope.myData.fromServer = data.d;
                    console.log($scope.myData.fromServer);
                })
                .error(function (data, status, headers, config) {
                    $scope.status = status;
                });
           }
      }).config(function ($httpProvider) {
          $httpProvider.defaults.headers.post = {};
          $httpProvider.defaults.headers.post["Content-Type"] = "application/json; charset=utf-8";
      });

And below is my c# method

    [WebMethod]
    public static string GetEmployees()
    {
        return "OK-test";
    }

Reference Get Json Data From Asp.Net Webmethod and Consume in angularJS

4
  • Did you check your Devtools => network panel to ensure the call went fine and returned some data? Commented Oct 5, 2016 at 14:35
  • @David the call went fine return url is something like this "localhost:35633/Account/…" Commented Oct 5, 2016 at 14:58
  • But, it has to return OK-test in your case right? Commented Oct 5, 2016 at 15:14
  • Ya It should return OK-test but its returning undefined Commented Oct 6, 2016 at 6:16

0

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.