Summary:
1) be familiar with loading and operating XML files by javascript;
Example of XML operations for DOM: http://www.w3school.com.cn/xmldom/met_document_getelementsbytagname.asp
2) in IE, you still need to use loadXML to convert to responseText;
3) asynchronous attribute settings after xml loading;
4) namespace processing and other issues;
The code below:
======= ASPX front-end code ========
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default. aspx. cs" Inherits = "_ Default" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div id = "div1">
</Div>
</Form>
<P> <input id = "Button1" type = "button" value = "button" onclick = "RequestWebService ();"/> </p>
<Script type = "text/javascript">
Var sUsrAgent = navigator. userAgent;
Var isIE = sUsrAgent. indexOf ("MSIE ")! =-1;
Var isIE6 = isIE & sUsrAgent. indexOf ("MSIE 6.0 ")! =-1;
Var isIE7 = isIE & sUsrAgent. indexOf ("MSIE 7.0 ")! =-1;
Var isFF = sUsrAgent. indexOf ("Firefox ")! =-1;
Var isOP = sUsrAgent. indexOf ("Opera ")! =-1;
Var isSF = sUsrAgent. indexOf ("Safari ")! =-1 & sUsrAgent. indexOf ("Chrome") =-1;
Var isCH = sUsrAgent. indexOf ("Chrome ")! =-1;
Var xmlHttp;
Function RequestWebService (){
// This is the address of the Web service we created in step 1.
Var URL = "http: // localhost: 3165/WebSite2/Service. asmx ";
// Splice them here
Var data;
Data = '<? Xml version = "1.0" encoding = "UTF-8"?> ';
Data = data + '<soap12: Envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: xsd = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope"> ';
Data = data + '<soap12: Body> ';
Data = data + '<HelloWorld xmlns = "http://tempuri.org/"/> ';
Data = data + '</soap12: Body> ';
Data = data + '</soap12: Envelope> ';
// Create an asynchronous object
XmlHttp = GetXmlHttpObject ();
XmlHttp. open ("POST", URL, false );
If (xmlHttp. overrideMimeType ){
XmlHttp. overrideMimeType ('text/xml ');
}
XmlHttp. SetRequestHeader ("Content-Type", "application/soap + xml ");
XmlHttp. onreadystatechange = stateChanged;
XmlHttp. Send (data );
}
Function stateChanged (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Alert (xmlHttp. getAllResponseHeaders ());
Alert (xmlHttp. responseText); // This has
// Var xmlDoc = xmlHttp. responseXML; // This is empty, but it will be shown below
Var xmlDoc = loadXMLDoc ();
XmlDoc. setProperty ("SelectionNamespaces", 'xmlns: soap = "http://www.w3.org/2003/05/soap-envelope" xmlns: ws = "http://tempuri.org /"');
// This section is the namespace (including display and anonymous) processing method, must be added!
Var node = xmlDoc. selectSingleNode ("/soap: Envelope/soap: Body/ws: HelloWorldResponse/ws: HelloWorldResult"); // if there is a value here, it will be OK. It will take one week before and after it!
Document. getElementById ("div1"). innerHTML = node. nodeTypedValue;
}
}
}
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;
}
Function loadXMLDoc (){
Var xmlDoc;
If (isIE ){
XmlDoc = getMSXmlParser ();
XmlDoc. async = false;
XmlDoc. loadXML (xmlHttp. responseText); // the webservice response must use loadXML
// XmlDoc. load (xmlHttp. responseText); // load the xml document
}
Else {
XmlDoc = xmlHttp. responseXML;
If (! XmlDoc ){
XmlDoc = (new DOMParser (). parseFromString (xmlHttp. responseText, 'text/xml ');
}
}
Return xmlDoc;
}
Function getMSXmlParser (){
Var parser = ['msxml2. DOMDocument.6.0 ',
'Msxml2. DOMDocument.5.0 ',
'Msxml2. DOMDocument.4.0 ',
'Msxml2. DOMDocument.3.0 ',
'Msxml2. domdocument ',
'Microsoft. XMLDOM ']; // the same as MSXML. DOMDocument
For (var I in parser ){
Try {
Var xParser = new ActiveXObject (parser [I]);
If (xParser ){
Return xParser;
}
}
Catch (e ){}
}
Return null;
}
</Script>
</Body>
</Html>
======= Backend CS file ========
In fact, there is no substantive content in this section.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
}
}
======= WebService code ========
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Services;
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
// To allow ASP. net ajax to call this Web service from a script, cancel the comments to the downstream.
// [System. Web. Script. Services. ScriptService]
Public class Service: System. Web. Services. WebService
{
Public Service (){
// If you use the designed component, uncomment the following line
// InitializeComponent ();
}
[WebMethod]
Public string HelloWorld (){
Return "Hello World ";
}
}
======= Returned responseText ========
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Soap: Envelope xmlns: soap = "http://www.w3.org/2003/05/soap-envelope"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: xsd = "http://www.w3.org/2001/XMLSchema">
<Soap: Body>
<HelloWorldResponse xmlns = "http://tempuri.org/">
<HelloWorldResult> Hello World </HelloWorldResult>
</HelloWorldResponse>
</Soap: Body>
</Soap: Envelope>
End!