任何人都可以指导我如何从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,config) { }); } catch (e) { throw e; }
我有同样的问题,我尝试了很多不同的方法,这是我发现它有效的方式……(我认为这些技巧是头配置和json参数与“data:{}”的组合,我是不确定,但这真的很棘手)
原文链接:https://www.f2er.com/angularjs/143386.html<%@ 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",$http) { $scope.myData = {}; $scope.myData.doClick = function (item,event) { $http.post('TestAngular.aspx/GetEmployees',{ data: {} }) .success(function (data,config) { $scope.myData.fromServer = data.d; }) .error(function (data,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>
在codebehid上的同一个aspx页面上,这是简单的代码……
[WebMethod] public static string GetEmployees() { return "OK-test"; }