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

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

本来觉得这个没必要写,但是还是有一些童鞋在问,就写一个吧。 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请求拦截跳转页面方案
Elasticsearch 随机返回数据 API
JSON格式讲解,JSON获取对象,JSONObject和JSONArray的操作
Jackson 转换JSON,SpringMVC ajax 输出,当值为null或者空不输出字段@JsonInclude
最新文章
PHP if/else/elseif 语句 40
HTML5 Canvas弧线教程 40
Java赋值运算符 118
XML内部实体和外部实体 201
Java面向对象编程概念 177
PHP回显语句 127
Linux—文件树 135
C语言while循环和do while循环 150
Python元组剖析 209
MySQL触发器教程 342
最热文章
最新MyEclipse8.5注册码,有效期到2020年 (已经更新) 682575
苹果电脑Mac怎么恢复出厂系统?苹果系统怎么重装系统? 674741
免费天气API,全国天气 JSON API接口,可以获取五天的天气预报 602531
免费天气API,天气JSON API,不限次数获取十五天的天气预报 579117
Jackson 时间格式化,时间注解 @JsonFormat 用法、时差问题说明 553084
我为什么要选择RabbitMQ ,RabbitMQ简介,各种MQ选型对比 509458
Elasticsearch教程(四) elasticsearch head 插件安装和使用 480074
Jackson 美化输出JSON,优雅的输出JSON数据,格式化输出JSON数据... ... 264843
Java 信任所有SSL证书,HTTPS请求抛错,忽略证书请求完美解决 244332
Elasticsearch教程(一),全程直播(小白级别) 225657
支付扫码

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

查看我的收藏

正在加载... ...