Preson.js File Contents
The code is as follows |
Copy Code |
[ { ' Name ': ' Xiangwen ' }, { ' Name ': ' Mengjie ' } ] XMLHttpRequest |
-json-ajax:[support IE7 above]
The code is as follows |
Copy Code |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <title>XHR</title> <style type= "Text/css" > . de{width:450px; height:450px border:4px solid #ccc;} . ok{width:450px; font-size:40px border-bottom:4px solid #ccc; display:block; text-decoration:none;} </style> <script type= "Text/javascript" > function Json (URL) { var ok = document.getElementById ("OK"); var json = document.getElementById ("JSON"); var xhr = new XMLHttpRequest (); Xhr.onreadystatechange = function () { if (xhr.readystate = = 4) { if ((Xhr.status >= && xhr.status <) | | xhr.status = = 304) { var cons = eval ("+ Xhr.responsetext +")); Ok.onclick = function () { json.innerhtml = "<li>" +cons[0].name+ "</li>"; } } } } Xhr.open ("Get", url, false); Xhr.send (NULL); } Window.onload = function () { Json ("Preson.js"); } </script> <body> <div class= "de" > <a href= "#" class= "OK" id= "OK" title= "JSON-XHR" >Json-XHR</a> <div class= "JSON" id= "JSON" ></div> </div> </body> |
Jquery-json-ajax:
The code is as follows |
Copy Code |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <title>untitled page</title> <style type= "Text/css" > . de{width:450px; height:450px border:4px solid #ccc;} . ok{width:450px; font-size:40px border-bottom:4px solid #ccc; display:block; text-decoration:none;} </style> <script type= "Text/javascript" src= "Jquery-1.6.4.min.js" ></script> <script type= "Text/javascript" > function Jsonurl (URL) { var $ok = $ ("#ok"); var $json = $ ("#json"); $ok. Click (function () { $.ajax ({ Type: "Get", Url:url, Beforesend:function (XMLHttpRequest) { }, Success:function (data, textstatus) { var Preson = eval ("+ Data +")); for (var i = 0; i < $json. length; i++) { var li = "<li>" + preson[i].name+ "</li>"; $json. Append (LI); } }, Error:function () { Alert ("Error"); } }); }); } $ (function () { Jsonurl ("Preson.js"); }); </script> <body> <div class= "de" > <a href= "#" class= "OK" id= "OK" title= "" >JSON-DATA</a> <div id= "JSON" class= "JSON" ></div> </div> </body> |
Okay, the above AJAX processing program only tested IE7 or above browser, the same I think in Ff,google browser should be normal use, as for IE6 no test, if there are questions you can comment on the message.