Ajax processing array, Ajax processing XML

Source: Internet
Author: User
AJAX JavaScript

This is the JavaScript code stored in the file "selectcustomer_xml.js:

var xmlHttpfunction showCustomer(str){ xmlHttp=GetXmlHttpObject();if (xmlHttp==null)  {  alert ("Your browser does not support AJAX!");  return;  }var url="getcustomer_xml.asp";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);}function stateChanged() { if (xmlHttp.readyState==4){var xmlDoc=xmlHttp.responseXML.documentElement;document.getElementById("companyname").innerHTML=xmlDoc.getElementsByTagName("compname")[0].childNodes[0].nodeValue;document.getElementById("contactname").innerHTML=xmlDoc.getElementsByTagName("contname")[0].childNodes[0].nodeValue;document.getElementById("address").innerHTML=xmlDoc.getElementsByTagName("address")[0].childNodes[0].nodeValue;document.getElementById("city").innerHTML=xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;document.getElementById("country").innerHTML=xmlDoc.getElementsByTagName("country")[0].childNodes[0].nodeValue;}}function GetXmlHttpObject(){var xmlHttp=null;try  {  // Firefox, Opera 8.0+, Safari  xmlHttp=new XMLHttpRequest();  }catch (e)  {  // Internet Explorer  try    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    }  catch (e)    {    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");    }  }return xmlHttp;}

ShowCustomer () and GetXmlHttpObject () are the same as those in the previous section. The stateChanged () function was used earlier in this tutorial. However, this time we use responseXML to return results in an XML document and use DOM to extract the value to be displayed.

AJAX Server Page

The server page called by JavaScript is a simple ASP file named "getcustomer_xml.asp.

This page is written in VBScript for Internet Information Server (IIS ). You can simply rewrite the page in PHP or other server languages.

See the corresponding example in PHP (test: missing a specific page ).

This code executes the SQL query for the database and returns the result in XML:

<%response.expires=-1response.contenttype="text/xml"sql="SELECT * FROM CUSTOMERS "sql=sql & " WHERE CUSTOMERID='" & request.querystring("q") & "'"on error resume nextset conn=Server.CreateObject("ADODB.Connection")conn.Provider="Microsoft.Jet.OLEDB.4.0"conn.Open(Server.Mappath("/db/northwind.mdb"))set rs=Server.CreateObject("ADODB.recordset")rs.Open sql, connif err <> 0 thenresponse.write(err.description)set rs=nothingset conn=nothingelseresponse.write("<?xml version='1.0' encoding='ISO-8859-1'?>")response.write("<company>")response.write("<compname>" &rs.fields("companyname")& "</compname>")response.write("<contname>" &rs.fields("contactname")& "</contname>")response.write("<address>" &rs.fields("address")& "</address>")response.write("<city>" &rs.fields("city")& "</city>")response.write("<country>" &rs.fields("country")& "</country>")response.write("</company>")end ifon error goto 0%>

Note the second line in the above ASP code: response. contenttype = "text/xml ". The ContentType attribute sets the HTTP content type for the response object. The default value of this attribute is "text/html ". This time, we set the content type to XML.

Then we select data from the database and use the data to build the XML document.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.