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
Devtools => networkpanel to ensure the call went fine and returned some data?OK-testin your case right?