ajax读取json数据
2014-03-22 10:40
399 查看
首先建立json.txt文件 { "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" } ] } 通过异步调用,来读取json数据 <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <script type="text/javascript"> var xmlHttp; function createXMLHttpRequest() { if(window.ActiveXObject) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } else if(window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } } function startRequest() { createXMLHttpRequest(); try { xmlHttp.onreadystatechange = handleStateChange; xmlHttp.open("GET", "Json.txt", true); xmlHttp.send(null); } catch(exception) { alert("xmlHttp Fail"); } } function handleStateChange() { if(xmlHttp.readyState == 4) { if (xmlHttp.status == 200 || xmlHttp.status == 0) { var result = xmlHttp.responseText; var json = eval("(" + result + ")"); alert(json.programmers[0].firstName);//读取json数据 //alert(json.sex); } } } </script> </head> <body> <div> <input type="button" value="AjaxTest" onclick="startRequest();" /> </div> </body> </html>
相关文章推荐
- 使用 ajax json(getJSON)从服务器读取数据,在IE下不更新问题解决
- 【B/S】AJAX读取Json数组里面的数据
- json格式数据取得1- 直接从client读取 (jquery+ajax实现)
- PHP读取目录下所有文件,并通过ajax返回json格式的数据
- AJAX读取json数据
- jquery简单ajax示例_读取json文件数据
- javascript 利用ajax读取json数据
- ajax读取本地json文件数据
- ASP.NET&AJAX&JSON - 动态读取数据
- jquery, highcharts, Ajax读取json数据
- ajax异服务器读取json数据
- 教你用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格式数据字符串出现回车时出错问题解决