Home > Web Front-end > JS Tutorial > body text

How to use Ajax and $.ajax

php中世界最好的语言
Release: 2018-04-02 15:46:45
Original
1352 people have browsed it

This time I will bring you how to use Ajax and $.ajax. What are the precautions when using Ajax and $.ajax? Here are practical cases, let’s take a look.

Example 1 (Basic creation format of Ajax request):



 Ajax练习(GET,不考虑浏览器兼容性)
 

 
 

    

 
Copy after login

 AjaxDemo实例
 
 

 
 

   
         

 
Copy after login

Example 2 (see attachment)

Consider browser compatibility for Ajax request processing and obtain the content of the background xml file.

Example 3 (see attachment)

Use $.Ajax to obtain background reading xml file content information.

function readXML1() {
   //创建XML对象
   var xmldom = new ActiveXObject("Microsoft.XMLDOM");
   //设置为异步
   xmldom.async = "false";
   //加载需要读取的XML文档
   xmldom.load("XML1.xml");
   info = "";
   //需要读取的根节点
   var node = xmldom.selectNodes("student");
   //依次读取其中的内容
   info = node[0].childNodes[0].nodeTypedValue + " 
" + node[0].childNodes[1].nodeTypedValue+ "
" + node[0].childNodes[2].nodeTypedValue;    document.getElementById("xmlmsg").innerHTML = info;   };
Copy after login
 function readXML2() {
   //实例化xml对象
   var xml = new ActiveXObject("Microsoft.XMLDOM");
   //异步设置
   xml.async = "false";
   //加载需要读取的XML文档
   xml.load("XML2.xml");
   info = "";
   //选择需要读取的对象名称
   var fnode = xml.documentElement.selectNodes("people");
   //循环输出文档的内容
   for (var i = 0; i < fnode.length; i++) {
    for (var j = 0; j < fnode[i].childNodes.length; j++) {
     info += fnode[i].childNodes[j].text + "
";     }    }    document.getElementById("xmlmsg").innerHTML = info;   };
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Ajax+mysql to create a message board function

The most basic way for ajax to realize three-level linkage concept

The above is the detailed content of How to use Ajax and $.ajax. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!