JQ 使用 Ajax 获取PHP生成的JSOP数据

之前写了一篇文章:PHP 生成JSON文件和获取JSON文件 打开链接

putjson.php

<?php
// 生成一个PHP数组
$data = array();
$data[0] = array('1','吴者然','onestopweb.cn');
$data[1] = array('2','何开','iteye.onestopweb.cn');
// 把PHP数组转成JSON字符串
$json_string = json_encode($data);
// 写入文件
file_put_contents('test.json', $json_string);
?>

生成的JSOP数据

[["1","\u5434\u8005\u7136","onestopweb.cn"],["2","\u4f55\u5f00","iteye.onestopweb.cn"]]

getjsop.html

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
</head>
<body>
<script type="text/javascript" src="jquery-1.10.1.min.js" ></script>
<script>
$(function(){
	$.ajax({ 
		url: 'test.json', 
		type: 'GET', 
		dataType: 'json', 
		//data: {param1: 'value1'}, 
		success: function (data){
			for(var i=0;i<data.length;i++){
				document.write(data[i][0]+" "+data[i][1]+" "+data[i][2]+" <br>");
			}
		} 
	})
});
</script>
</body>
</html>

效果图:

 

 使用点击事件的处理方式

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
</head>
<body>
<h3>阅谁问君诵,水落清香浮</h3>
<button id="btn">加载数据</button>
<p id="cont"></p>
<script type="text/javascript" src="jquery-1.10.1.min.js" ></script>
<script>
$(function(){
	$("#btn").click(function(){
		$.ajax({ 
			url: 'test.json', 
			type: 'GET', 
			dataType: 'json', 
			//data: {param1: 'value1'}, 
			success: function (data){
				var getCont="";
				for(var i=0;i<data.length;i++){
					getCont+=data[i][0]+" "+data[i][1]+" "+data[i][2]+"<br>";
				}
				$("#cont").html(getCont);
			} 
		})
	});
});
</script>
</body>
</html>

 效果图:

 

扫描二维码关注公众号,回复: 351134 查看本文章

猜你喜欢

转载自onestopweb.iteye.com/blog/2333290