Introduce AJAX about how to dynamic loading HTML/JSON/XML da
发布时间:2020-12-16 00:49:39 所属栏目:百科 来源:网络整理
导读:1. how to loading HTML/JSON/XML dataType via AJAX As we know,we can d ynamic loading the file via ajax,it is very convient for customer. And,there are three kinds of dataType: JSON,XML,HTML. but the method to call file is different,here is
1. how to loading HTML/JSON/XML dataType via AJAXAs we know,we can dynamic loading the file via ajax,it is very convient for customer. And,there are three kinds of dataType: JSON,XML,HTML. but the method to call file is different,here is themy demo. This is my demo indx page. this is code of the Index: <html> <body> <h2>This is AJAX Example</h2> <button type="button" id="btn">post html file</button> <button type="button" id="btn2">post json file </button> <button type="button" id="btn3">post xml file</button> <br/> <br/> <div id="myDiv">This is ajax result</div> </body> </html> Note: Because I had use two kind of methods to call the AJAX,then hiddencode is equal to the $.ajax({}) function,it means you use the hidden code instead of the $.ajax({}) function.2.Dynamic loading html file.a.Create a html file to store some div,this will load in index page:<div class="right">This is html</div> <div>This is html number two</div> <div>This is html number three</div> b. add script code in head tag<head> <script src="http://code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function () { $.ajax({ url:"1.html",type: "get",dataType:"html",success: function (data) { $("#myDiv").load("1.html .right"); } }); //$("#myDiv").load("1.html .right"); return false; }); </head> c. Run the index page and click the button named "post html file",here is the result:
2.Dynamic loading JSON file.a. Create a json file extend name is 2.json[ { "name":"Tristan","number":"001" },{ "name":"Tristan2","number":"003" },{ "name":"Tristan3","number":"002" } ] b. Add click event to head$("#btn2").click(function() { /* $.getJSON("2.json",function(data) { var html = ""; $.each(data,function(index,key) { html += "<div>" + key.name + "</div>"; html += "<div>" + key.number + "</div>"; }); $("#myDiv2").append(html); }); */ $.ajax({ url: "2.json",dataType:"json",success: function (data) { var html = ""; $.each(data,function (index,key) { html += "<div>" + key.name + "</div>"; html += "<div>" + key.number + "</div>"; }); $("#myDiv").append(html) ; } }); return false; });
|
推荐文章
站长推荐
- 有没有简单的方法来远程调试在(develpoment)服务
- 正则表达式 – 解析mysql:/// sqlite:/// URL
- c – 为什么必须将SetWindowsHookEx与Windows消息
- c# – 即使CausesValidation为false,也检查页面是
- Fragment异常:Binary XML file line #8: Error i
- 硬币问题-动态规划详解
- c# – 如何创建web.config文件以加载SoapExtensi
- Flex4.x安装SVN
- Swift 范的 CGRect、CGSize 和 CGPoint(转)
- Flex ActionScript 3.0 SharedObject 用于在用户
热点阅读