JQuery Ajax四种写法,Ajax请求返回JSON 操作Demo

JSON 2016-09-09 13:22:41 58100

本来觉得这个没必要写,但是还是有一些童鞋在问,就写一个吧。 Ajax  支持多种数据请求类型。这篇博客就说说请求 JSON  吧。不过 Ajax  不是原生的,是基于 jQuery  Ajax  。我下面展示一下常用的四种 Ajax  写法。

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<title>N种 Ajax请求返回JSON 操作Demo。</title>
<script src="http://open.sojson.com/domain/js/jquery-2.1.4.min.js"></script>
<script>
	//ajax 方式
	$.ajax({
		url:"http://bang.cdn.www.sojson.com/testJSON.json",
		dataType:"json",
		type:"get",
		success:function(result){
			console.log("status:%s", result.status);
			$.each(result.data,function(){
				console.log("name:%s,age:%s",this.name,this.age)
			});
		}
	});
	//post 方式
	$.post("http://bang.cdn.www.sojson.com/testJSON.json",{},function(result){
		console.log("status:%s", result.status);
		$.each(result.data,function(){
			console.log("name:%s,age:%s",this.name,this.age)
		});
	},"json");
	//get 方式
	$.get("http://bang.cdn.www.sojson.com/testJSON.json",{},function(result){
		console.log("status:%s", result.status);
		$.each(result.data,function(){
			console.log("name:%s,age:%s",this.name,this.age)
		});
	},"json");
	//get JSON 方式
	$.getJSON("http://bang.cdn.www.sojson.com/testJSON.json",{},function(result){
		console.log("status:%s", result.status);
		$.each(result.data,function(){
			console.log("name:%s,age:%s",this.name,this.age)
		});
	});
</script>
</head>
<body>
</body>
</html>


版权所属:SO JSON在线解析

原文地址:https://www.sojson.com/blog/170.html

转载时必须以链接形式注明原始出处及本声明。

本文主题:

如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。

关于作者
一个低调而闷骚的男人。
相关文章
JSOUP 教程,JSOUP请求JSON ,JSOUP返回JSON 数据
Java API接口返回不是JSON的解决方案,SpringMVC返回JSON配置。
Httpclent 请求限制,判断返回类型和返回数据大小
CDN 请求返回 connection reset by peer,被拦截请求解决方案
Java 之 HTTP请求乱码解决,GZIP 返回值乱码解决
Elasticsearch 聚合(aggregation)查询返回所有
Shiro 教程,Ajax请求拦截跳转页面方案
Jackson 转换JSON,SpringMVC ajax 输出,当值为null或者空不输出字段@JsonInclude
Elasticsearch 随机返回数据 API
json 解析与生成工具类 ,JSON操作讲解(附件)
最新文章
Linux I/O重定向 1767
Ruby 循环 - while、for、until、break、redo 和 retry 711
Node.js:全局对象 517
如何使用终端检查Linux上的内存使用情况 635
JavaScript对象详细剖析 300
Python print() 函数 409
PHP if/else/elseif 语句 407
HTML5 Canvas弧线教程 387
Java赋值运算符 431
XML内部实体和外部实体 464
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 686836
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 675081
免费天气API,天气JSON API,不限次数获取十五天的天气预报 615717
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 611106
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 555623
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 510028
Elasticsearch教程(四) elasticsearch head 插件安装和使用 481399
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 269205
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244787
Elasticsearch教程(一),全程直播(小白级别) 227489
支付扫码

所有赞助/开支都讲公开明细,用于网站维护:赞助名单查看

查看我的收藏

正在加载... ...