日期:2014-05-16 浏览次数:20688 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> function createXHR() { var request = false; try { request = new ActiveXObject('Msxml2.XMLHTTP'); } catch (err2) { try { request = new ActiveXObject('Microsoft.XMLHTTP'); } catch (err3) { try { request = new XMLHttpRequest(); } catch (err1) { request = false; } } } return request; } function loadJSON(fname) { var xhr = createXHR(); xhr.open("GET", fname, true); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status != 404) { [color=#FF0000]var data = eval("(" + xhr.responseText + ")"); //到这时还有值 document.getElementById("zone").innerHTML = "<h2>Items:</h2>";//再走就变成undefined[/color] for (i = 0; i < 3; i++) { document.getElementById("zone").innerHTML += data.listing.items[i].description + ', price <i>' + data.listing.items[i].price + "</i><br/>"; } } else { document.getElementById("zone").innerHTML = fname + " not found"; } } } xhr.send(null); } </script> <title>Ajax Get JSON Demo</title></head> <body bgcolor="#FFFFFF"> <p><font size="+3">Ajax JSON/JAXB Demo</font></p> <hr> <FORM name="ajax" method="POST" action=""> <p> <INPUT type="BUTTON" value=" Click to load the JSON file " ONCLICK="loadJSON('resteasy/tutorial/items')"> </p> </FORM> <div id="zone"></div> </body> </html>