两强相争,鹿死谁手 — JQuery中的Ajax与AngularJS中的$http
一、JQuery与AngularJS
首先,先简单的了解一下JQuery与AngularJS。从源头上来说,两者都属于原生JS所封装成的库,两种为平行关系。
二、Ajax请求与数据遍历打印
这里是Ajax和$http请求的JSON文件概览,默认的路径我们就放在与两者同级的文件夹里。
[
{
"name": "一号",
"age": 17,
"hobby": [
"吃",
"喝"
],
"score":{
"math":78,
"chinese":89
}
},
{
"name": "二号",
"age": 17,
"hobby": [
"喝",
"玩"
],
"score":{
"math":78,
"chinese":89
}
},
{
"name": "三号",
"age": 17,
"hobby": [
"玩",
"乐"
],
"score":{
"math":78,
"chinese":89
}
},
{
"name": "四号",
"age": 17,
"hobby": [
"吃",
"喝",
"玩",
"乐"
],
"score":{
"math":78,
"chinese":89
}
}
]
下面是Ajax请求获取JSON文件的代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Ajax</title> <script language="JavaScript" src="js/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function(){ $("button").click(function(){ $.ajax({
type:"post",
url:"http://localhost:8080/JSON/h51701.json",
dataType:"JSON",
success:function(data){
console.log(data)
}
}); }) </script> </head>
<body> <button>点击请求JSON文件</button> <div></div> </body>
</html>
如果想要直接遍历取出JSON文件中的各种值,则可以通过post和get,一般我们所用的是post,使用时,只需要吧$ajax这一部分换成以下代码。
$.post("http://localhost:8080/JSON/h51701.json",{},function(data){
for(var i = 0 ; i < data.length ; i++){
$("div").append("姓名:"+data[i].name+"<br />");
$("div").append("年龄:"+data[i].age+"<br />");
$("div").append("数学成绩:"+data[i].score.math+"<br />");
$("div").append("语文成绩:"+data[i].score.chinese+"<br />");
$("div").append("爱好:"+data[i].hobby.toString()+"<br /><br />");
}
},"json")
在这里,我们一般是采用循环遍历的方法一一取出。
三、$http请求与数据的提取
相较而言,$http的方法更简单粗暴,代码如下。
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title>$http</title>
</head> <body ng-app="app" ng-controller="ctrl"> <pre>{{data}}</pre> <table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>喜好</th>
<th>数学成绩</th>
<th>语文成绩</th>
<th>总分</th>
</tr>
</thead>
<tr ng-repeat="item in data | orderBy:'score.chinese'">
<td ng-bind="item.name"></td>
<td ng-bind="item.age"></td>
<td ng-bind="item.hobby"></td>
<td ng-bind="item.score.chinese"></td>
<td ng-bind="item.score.math"></td>
<td ng-bind="item.score.chinese + item.score.math"></td>
</tr>
</table> </body> <script src="libs/angular.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
angular.module("app", [])
.controller("ctrl", function($scope,$http) {
//方法一
$http({
method: 'POST',
url: 'h51701.json'
}).then(function successCallback(response) {
// 请求成功执行代码
$scope.res = response;
}, function errorCallback(response) {
// 请求失败执行代码
alert("服务请求失败")
});
//方法二
$http.get("h51701.json").then(function successFunction(a){
$scope.res = a;
})
//方法三
$http.post("h51701.json",{/*传递的参数对象*/}).then(function successFunction(a){
$scope.data = a.data;//从返回的信息中取出需要的数据,为JSON对象(数组)
}) }) </script> </html>
在请求方面,三种方法大致与ajax相同,但是在每一数据的提取方面,AngularJS所提供的ng-repeat提供了非常大的便利。
综上,两者相比较,还是AngularJS提取更方便。但是从现如今的更新上讲,JQuery中的ajax更加稳定。
两强相争,鹿死谁手 — JQuery中的Ajax与AngularJS中的$http的更多相关文章
- jQuery AutoComplete在AJAX UpdatePanel环境中PostBack之后无法工作
前些日子,Insus.NET有实现<ASP.NET MVC使用jQuery实现Autocomplete>http://www.cnblogs.com/insus/p/5638895.htm ...
- 从jquery里的$.ajax()到angularjs的$http
jquery中对ajax的使用做了很多封装,使得我们使用习惯了,反而并不大清楚在请求过程中的一些细节. 在第一次使用angularjs的$http时,后台一直接受不到前端请求的数据,于是小小研究了一下 ...
- 一个方法中的ajax在success中renturn一个值,但是方法的返回值是undefind?
https://segmentfault.com/q/1010000003762379 A页面 console.log(handleData("search_list", &quo ...
- 在js中,ajax放在for中,ajax获取得到的变量有误
先看代码 for(var i=0;i<tds.length;i++){ mui.ajax(url+'api/client/gifts/isSigned', {data :{ sqId:" ...
- 使用ionic中的侧边栏以及angularjs中广播的使用
接着之前的ionic的例子 查看例子:我的第一段ionic代码 demo3.html(黄底内容为增加或修改的内容) <!DOCTYPE html> <html ng-app=&quo ...
- AngularJS学习--- AngularJS中XHR(AJAX)和依赖注入(DI) step5
前言:本文接前一篇文章,主要介绍什么是XHR,AJAX,DI,angularjs中如何使用XHR和DI. 1.切换工具目录 git checkout -f step- #切换分支 npm start ...
- django中的ajax组件
目录 django中的ajax 向服务器发送请求的途径 Ajax的特点 基于jquery实现的ajax请求 利用ajax实现计算器 利用ajax实现登陆认证 利用form表单进行文件上传 利用ajax ...
- Angularjs 中使用指令绑定点击事件
项目中,模板中的菜单是jQuery控制的,在Angularjs中就运行不到了,因为菜单项是ng-repeat之后的. 如html <ul id="main-menu"> ...
- 转: 理解AngularJS中的依赖注入
理解AngularJS中的依赖注入 AngularJS中的依赖注入非常的有用,它同时也是我们能够轻松对组件进行测试的关键所在.在本文中我们将会解释AngularJS依赖注入系统是如何运行的. Prov ...
随机推荐
- 监听器的小示例:利用HttpSessionListener和HttpServletContextListener实现定时销毁HttpSession
1.创建MyServletContextListener实现HttpServletContextListener接口 @Override public void contextDestroyed(Se ...
- 傻瓜式使用AutoFac
定义一个接口: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespa ...
- while循环语句的几种方式
我们知道,在Python中经常我们要使用循环,其中最常用的是while循环,while有很多结合方式,我们知道,如果一个循环没有结束语句那么就失去了意义,所以我们一定要有结束语句,下面来看看while ...
- 如何解决chrome 等浏览器不支持本地ajax请求的问题
XMLHttpRequest cannot load file:///D:/WWW/angularlx/ui-router-test/template/content.html. Cross orig ...
- lib-flexble 使用遇到的bug及解决方案
1 lib-flexble解决微信端长按不能弹出识别二维码功能 加viewport就完美解决 flexble可以自动完成 一般情况不建议加 但是么 bug出来了 加上就好了 2 Font Boos ...
- [工具技巧] SecureCRT使用技巧 V1.0
本文档适用对象为需要使用secureCRT做系统或网络等调试的工程师,其必须有用过该款软件,基础功能会使用.对于那些需要修改大量设备的配置时像远程升级等等,更应该学习本文档. 1 Secu ...
- poj2352树状数组
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- jquery的ajax与spring mvc对接注意事项
昨天一直纠结这么一个问题,应用场景是这样的: 这里登陆是通过jquery的ajax传输数据到后台controller类相应的映射mapping接收.本来是想,在后台验证成功之后返回一个视图modelA ...
- java:Comparator比较器
/*Comparator是java.util包里的一个接口,使用时应该实现导入相应的包, *再写一个实现了Comparator接口的类,并复写int compare(Object o)方法, *写出你 ...
- mybatis mapper.xml 写关联查询 运用 resultmap 结果集中 用 association 关联其他表 并且 用 association 的 select 查询值 报错 java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for mybatis.map
用mybaits 写一个关联查询 查询商品表关联商品规格表,并查询规格表中的数量.价格等,为了sql重用性,利用 association 节点 查询 结果并赋值报错 商品表的mapper文件为Gooo ...