AngularJS学习篇(九)
2024-09-16 14:57:47
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);
}
最新文章
- 非对称技术栈实现AES加密解密
- C#-ASP.NET MVC-架构【1】-自定义错误页
- sql高级语句大全
- 在一台机器上模拟mongodb分片
- jquery获取所有被选中checkbox
- Android中Adapter之BaseAdapter使用
- RPC、SQL、NFS属于OSI的哪一层
- ubuntu16.04 搭建 Mysql服务器
- JAVA 异常对于性能的影响
- [CSS]学习总结
- Flasback数据库(闪回数据库)
- (三)跟我一起玩Linux网络服务:DHCP服务配置之主服务器配置
- JDBC基本知识
- SEOer怎样安排一天的工作
- Light OJ 1095 Arrange the Numbers(容斥)
- Advanced Sort Algorithms
- IIS异常:CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\Framework\.。。”--“拒绝访问
- docker save提示no space left on device错误
- NodeJS笔记(三)-创建第一个NodeJS web项目 Express
- 负载均衡----实现配置篇(Nginx)