Ajax responsexml instance

Source: Internet
Author: User

Unlike responsetext, which returns an HTTP response as a string, responsexml returns a response in XML.

The responsexml attribute returns an XML document object. You can use the W3C Dom node tree method and attributes to check and parse this object.

Ajax responsexml instance

In the following Ajax example, we will demonstrate how to use Ajax technology to read information from the database. This time, the data selected from the database will be converted to an XML document, and then we will use Dom to extract the value to be displayed.

Select a name from the drop-down list

Ajax instance explanation

The preceding example contains an HTML form, several <span> elements that retain the returned data, and links to a piece of javascript:

<HTML> <SCRIPT src = "selectcustomer_xml.js"> </SCRIPT></Head> <body><Form action = ""><Label> select Customer: <selectName = "customers" onchange = "showcustomer (this. Value )"> <Option value = "alfki"> Alfreds futterkiste </option> <option value = "norts"> North/South </option> <option value = "wolza"> Wolski zajazd </option> </SELECT> </label></Form><B> <span id = "companyName"> </span> </B> <br/> <span id = "contactname"> </span> <br/> <SPAN id = "Address"> </span> <span id = "city"> </span> <br/> <span id = "country"> </span> </body>  

The preceding example contains an HTML form with a drop-down box named "MERs.

When you select a customer from the drop-down list, the function "showcustomer ()" is executed. The event "onchange" triggers the function execution. In other words, the showcustomer () function is called whenever you change the value in the drop-down box.

Javascript is listed belowCode.

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 xmldockers xmlhttp.responsexml.doc umentelement; 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 MERs" 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. descr Iption) 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.