AJAX读取json数据
2013-11-14 15:09
573 查看
转自:http://blog.csdn.net/u011955534/article/details/15942181
1、创建一个json.txt文件
[plain]
view plaincopy
{
"programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
}
2、jsp页面
[html]
view plaincopy
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
var xmlHttp=null;
//创建xmlhttprequest对象
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveObject("Microsoft.XMLHTTP");
}
function myclick(){
try{
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("get","json.txt",true);
xmlHttp.send();
}
catch(exception){
alert("xmlHttp fail");
}
}
function handleStateChange(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
var result=xmlHttp.responseText;
var json=eval("("+result+")");
alert(json.programmers[0].firstName);
}
}
</script>
</head>
<body>
<div>
<input type="button" value="AjaxTest" onclick="myclick()"/>
</div>
</body>
</html>
1、创建一个json.txt文件
[plain]
view plaincopy
{
"programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "brett@newInstance.com" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "jason@servlets.com" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "elharo@macfaq.com" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
]
}
2、jsp页面
[html]
view plaincopy
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript">
var xmlHttp=null;
//创建xmlhttprequest对象
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveObject("Microsoft.XMLHTTP");
}
function myclick(){
try{
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("get","json.txt",true);
xmlHttp.send();
}
catch(exception){
alert("xmlHttp fail");
}
}
function handleStateChange(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
var result=xmlHttp.responseText;
var json=eval("("+result+")");
alert(json.programmers[0].firstName);
}
}
</script>
</head>
<body>
<div>
<input type="button" value="AjaxTest" onclick="myclick()"/>
</div>
</body>
</html>
相关文章推荐
- 教你用jQuery 和 Ajax 解析JSON数据。读取XHR妥妥的
- java 读取 ajax post 到后台的Json数据
- Ajax读取XML和JSON数据
- AJAX读取json数据
- 在后台流中读取的数据如果是字节,请确认在ajax中传入的data数据是Json格式
- 使用js的ajax方法读取txt文本里面的JSON数据并追加到Html元素节点上
- ajax实现的二级联动_读取的是json格式数据
- 如何使用ajax读取Json中的数据
- ajax读取json数据
- 使用 ajax json(getJSON)从服务器读取数据,在IE下不更新问题解决
- json格式数据取得1- 直接从client读取 (jquery+ajax实现)
- 通过ajax读取json格式数据字符串出现回车时出错问题解决
- 利用ajax的getJSON读取数据时注意的问题
- jQuery+ajax读取json数据并按照价格排序示例
- 关于ajax读取数据表中存放复合json的问题
- 使用 ajax json(getJSON)从服务器读取数据,在IE下不更新问题解决
- 【B/S】AJAX读取Json数组里面的数据
- PHP读取目录下所有文件,并通过ajax返回json格式的数据
- jquery简单ajax示例_读取json文件数据
- javascript 利用ajax读取json数据