1. how to loading HTML/JSON/XML dataType via AJAX

As 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;
});


c. Click the button named "psot json file" and get the result:

2.Dynamic loading XML file.

a. Create a xml file extend name 3.xml

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>

b. Addclick functionin head

$("#btn3").click(function () {
    
    $.ajax({
        url: "3.xml",dataType:"xml",success: function (data) {
            $(data).find("book").each(function () {
                html = "";
                html += "<div>" + $(this).find("title").text() + "</div>";
                $("#myDiv").append(html);
            });
        }

    });
  
/*
    $.get("3.xml",function (data) {
        $(data).find("book").each(function () {
            html = "";
            html +="<div>" +  $(this).find("title").text() + "</div>";
            $("#myDiv3").append(html);
        });

    })
   */
    return false;
});


c. The result:


2. here is the completed code:

<html>
<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",success: function (data) {
                $("#myDiv").load("1.html .right");
            }

        });

//$("#myDiv").load("1.html .right");
return false;
});

$("#btn2").click(function() {

 /*
    $.getJSON("2.json",key) {
                html += "<div>" + key.name + "</div>";
                html += "<div>" + key.number + "</div>";
            });
            $("#myDiv").append(html) ;
            
        }
    });
    
    return false;
});

$("#btn3").click(function () {
    
    $.ajax({
        url: "3.xml",success: function (data) {
            $(data).find("book").each(function () {
                html = "";
                html += "<div>" + $(this).find("title").text() + "</div>";
                $("#myDiv").append(html);
            });
        }

    });
    /*
    $.get("3.xml",function (data) {
        $(data).find("book").each(function () {
            html = "";
            html +="<div>" +  $(this).find("title").text() + "</div>";
            $("#myDiv3").append(html);
        });

    })
   */
    return false;
});

});

</script>
</head>
<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>


More to link: http://www.w3school.com.cn/ajax/index.asp

Introduce AJAX about how to dynamic loading HTML/JSON/XML dataType的更多相关文章

  1. HTML5 input新增type属性color颜色拾取器的实例代码

    type 属性规定 input 元素的类型。本文较详细的给大家介绍了HTML5 input新增type属性color颜色拾取器的实例代码,感兴趣的朋友跟随脚本之家小编一起看看吧

  2. canvas中普通动效与粒子动效的实现代码示例

    canvas用于在网页上绘制图像、动画,可以将其理解为画布,在这个画布上构建想要的效果。本文详细的介绍了粒子特效,和普通动效进行对比,非常具有实用价值,需要的朋友可以参考下

  3. amazeui模态框弹出后立马消失并刷新页面

    这篇文章主要介绍了amazeui模态框弹出后立马消失并刷新页面,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  4. 移动HTML5前端框架—MUI的使用

    这篇文章主要介绍了移动HTML5前端框架—MUI的使用的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  5. H5混合开发app如何升级的方法

    本篇文章主要介绍了H5混合开发app如何升级的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  6. canvas学习和滤镜实现代码

    这篇文章主要介绍了canvas学习和滤镜实现代码,利用 canvas,前端人员可以很轻松地、进行图像处理,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  7. localStorage的过期时间设置的方法详解

    这篇文章主要介绍了localStorage的过期时间设置的方法详解的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  8. 详解HTML5 data-* 自定义属性

    这篇文章主要介绍了详解HTML5 data-* 自定义属性的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. HTML5的postMessage的使用手册

    HTML5提出了一个新的用来跨域传值的方法,即postMessage,这篇文章主要介绍了HTML5的postMessage的使用手册的相关资料,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  10. 教你使用Canvas处理图片的方法

    本篇文章主要介绍了教你使用Canvas处理图片的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

随机推荐

  1. xe-ajax-mock 前端虚拟服务

    最新版本见Github,点击查看历史版本基于XEAjax扩展的Mock虚拟服务插件;对于前后端分离的开发模式,ajax+mock使前端不再依赖后端接口开发效率更高。CDN使用script方式安装,XEAjaxMock会定义为全局变量生产环境请使用xe-ajax-mock.min.js,更小的压缩版本,可以带来更快的速度体验。

  2. vue 使用 xe-ajax

    安装完成后自动挂载在vue实例this.$ajaxCDN安装使用script方式安装,VXEAjax会定义为全局变量生产环境请使用vxe-ajax.min.js,更小的压缩版本,可以带来更快的速度体验。cdnjs获取最新版本点击浏览已发布的所有npm包源码unpkg获取最新版本点击浏览已发布的所有npm包源码AMD安装require.js安装示例ES6Module安装通过Vue.use()来全局安装示例./Home.vue

  3. AJAX POST数据中文乱码解决

    前端使用encodeURI进行编码后台java.net.URLDecoder进行解码编解码工具

  4. Koa2框架利用CORS完成跨域ajax请求

    实现跨域ajax请求的方式有很多,其中一个是利用CORS,而这个方法关键是在服务器端进行配置。本文仅对能够完成正常跨域ajax响应的,最基本的配置进行说明。这样OPTIONS请求就能够通过了。至此为止,相当于仅仅完成了预检,还没发送真正的请求呢。

  5. form提交时,ajax上传文件并更新到&lt;input&gt;中的value字段

  6. ajax的cache作用

    filePath="+escape;},error:{alert;}});解决方案:1.加cache:false2.url加随机数正常代码:网上高人解读:cache的作用就是第一次请求完毕之后,如果再次去请求,可以直接从缓存里面读取而不是再到服务器端读取。

  7. 浅谈ajax上传文件属性contentType = false

    默认值为contentType="application/x-www-form-urlencoded".在默认情况下,内容编码类型满足大多数情况。在这里,我们主要谈谈contentType=false.在使用ajax上传文件时:在其中先封装了一个formData对象,然后使用post方法将文件传给服务器。说到这,我们发现在JQueryajax()方法中我们使contentType=false,这不是冲突了吗?这就是因为当我们在form标签中设置了enctype=“multipart/form-data”,

  8. 909422229_ajaxFileUpload上传文件

    ajaxFileUpload.js很多同名的,因为做出来一个很容易。我上github搜AjaxFileUpload出来很多类似js。ajaxFileUpload是一个异步上传文件的jQuery插件传一个不知道什么版本的上来,以后不用到处找了。语法:$.ajaxFileUploadoptions参数说明:1、url上传处理程序地址。2,fileElementId需要上传的文件域的ID,即的ID。3,secureuri是否启用安全提交,默认为false。4,dataType服务器返回的数据类型。6,error

  9. AJAX-Cache:一款好用的Ajax缓存插件

    原文链接AJAX-Cache是什么Ajax是前端开发必不可少的数据获取手段,在频繁的异步请求业务中,我们往往需要利用“缓存”提升界面响应速度,减少网络资源占用。AJAX-Cache是一款jQuery缓存插件,可以为$.ajax()方法扩展缓存功能。

  10. jsf – Ajax update/render在已渲染属性的组件上不起作用

    我试图ajax更新一个有条件渲染的组件。我可以确保#{user}实际上是可用的。这是怎么引起的,我该如何解决呢?必须始终在ajax可以重新呈现之前呈现组件。Ajax正在使用JavaScriptdocument.getElementById()来查找需要更新的组件。但是如果JSF没有将组件放在第一位,那么JavaScript找不到要更新的内容。解决方案是简单地引用总是渲染的父组件。

返回
顶部