zoukankan      html  css  js  c++  java
  • AngularJS学习篇(九)

    AngularJS XMLHttpRequest

    $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。

    $http.get('someUrl',config).then(successCallback,errorCallback);
    $http.post('someUrl',data,config).then(successCallback,errorCallback);

    废弃声明 (v1.5)

    v1.5 中$http 的 success 和 error 方法已废弃。使用 then 方法替代。

    客户端代码:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <script src="angular-1.6.3/angular.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myCtrl">
    <ul>
    <li>{{names.name+' '+ names.gender}}</li>
    </ul>
    </div>
    <script>
    var app = angular.module('myApp', [])
    .config(['$sceDelegateProvider', function($sceDelegateProvider) {
    // We must whitelist the JSONP endpoint that we are using to show that we trust it
    $sceDelegateProvider.resourceUrlWhitelist([
    'self',
    'http://localhost:58993/**' //服务器端运行的URL
    ]);
    }]);//很重要,要先设置为信任的url
    app.controller('myCtrl', ['$scope', '$http', '$templateCache',
    function($scope, $http) {
    $http.jsonp('http://localhost:58993/home/TestJSONP?name=2').
    then(function(response) {
    $scope.names = response.data;
    }, function(response) {
    alert(response.data)
    });
    }]);
    </script>
    </body>
    </html>

    注意:以上代码的 JSONP 跨域请求。

    C#服务端代码

    public void TestJSONP()
    {
      string callback = Request.QueryString["callback"];
      var name = Request.QueryString["name"];
      var json = "{'name':2,'gender':'男'}";
      //JSONP格式:回调函数名(json格式参数)
      string result = string.Format("{0}({1})", callback, json);
      Response.ContentType = "application/json";
      Response.Write(result);
    }    
  • 相关阅读:
    mybatis3.4.3中文文档(.chm文件)
    Castle学习系列之二:Castle配置
    自己写一个jquery
    apply bind call 和 this
    超级无敌海景拽掉咋天的魔方
    小试Dapper
    Linux或Linux虚拟机桥接模式使用Python2认证Drcom
    分治法实现 n皇后问题 Java语言
    分治法实现1-N的数字按字典序全排列组合 Java语言
    Java实现二进制转换16进制(可以去掉空格)
  • 原文地址:https://www.cnblogs.com/dehuachenyunfei/p/6642612.html
Copyright © 2011-2022 走看看