加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

从angularJS中的Asp.Net Webmethod和Consume获取Json数据

发布时间:2020-12-17 07:52:17 所属栏目:安全 来源:网络整理
导读:任何人都可以指导我如何从asp.net webmethod获取json数据,并在angularJS中使用它. app.controller('MainController',['$scope',function ($scope,$http) { try { $http({ method: 'GET',url: 'ProblemList.aspx/GetProblemList' }).success(function (data,s
任何人都可以指导我如何从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:{}”的组合,我是不确定,但这真的很棘手)
<%@ 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";
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读