我正在尝试使用aspx
从AngularJS
后端到http post
获取字符串,但它返回undefined。
这是我的代码
<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";
});
以下是我的c#
方法
[WebMethod]
public static string GetEmployees()
{
return "OK-test";
}
参考Get Json Data From Asp.Net Webmethod and Consume in angularJS