Ama*_*han 3 asp.net webmethod angularjs
任何人都可以指导我如何从asp.net webmethod获取json数据,并在angularJS中使用它.
app.controller('MainController', ['$scope', function ($scope, $http) {
try {
$http({ method: 'GET', url: 'ProblemList.aspx/GetProblemList' })
.success(function (data, status, headers, config) {
alert(data); }).error(function (data, status, headers, config) {
});
} catch (e) {
throw e;
}
Run Code Online (Sandbox Code Playgroud)
我有同样的问题,我尝试了很多不同的方法,这是我发现它的工作方式......(我认为技巧是头配置和json参数与"data:{}"的组合,我是不确定,但这真的很棘手)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestAngular.aspx.cs" Inherits="COEWebApp.NCoe.Employees.TestAngular" %>
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
</head>
<body ng-app="myapp">
<div ng-controller="MyController" >
<button 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('TestAngular.aspx/GetEmployees', { data: {} })
.success(function (data, status, headers, config) {
$scope.myData.fromServer = data.d;
})
.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";
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在codebehid上的同一个aspx页面上,这是简单的代码......
[WebMethod]
public static string GetEmployees()
{
return "OK-test";
}
Run Code Online (Sandbox Code Playgroud)