先總結幾個要領:
1)要熟悉javascript對XML文件的加載與操作;
DOM的XML操作可參考的示例:http://www.w3school.com.cn/xmldom/met_document_getelementsbytagname.asp
2)在IE下面還是要通過loadXML來轉responseText;
3)xml加載后異步屬性設置;
4)命名空間處理等問題;
下面上代碼:
========ASPX前臺代碼========
復制代碼 代碼如下:
%@ 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() {
//這是我們在第一步中創(chuàng)建的Web服務的地址
var URL = "http://localhost:3165/WebSite2/Service.asmx";
//在這處我們拼接
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>';
//創(chuàng)建異步對象
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); // 這個有
//var xmlDoc = xmlHttp.responseXML; // 這個是空的,但下面會讓它出來
var xmlDoc = loadXMLDoc();
xmlDoc.setProperty("SelectionNamespaces", 'xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ws="http://tempuri.org/" ');
// 這面這段是命名空間(包含顯示與匿名的兩種)處理方法,必須加上!
var node = xmlDoc.selectSingleNode("/soap:Envelope/soap:Body/ws:HelloWorldResponse/ws:HelloWorldResult"); //這邊能有值就OK了,為了它前后消耗了1周時間!
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); //webservice response 需要用loadXML
//xmlDoc.load(xmlHttp.responseText); // 加載xml文檔需要用load
}
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>
========后臺CS文件========
其實這段沒有實質內容
復制代碼 代碼如下:
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代碼========
復制代碼 代碼如下:
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)]
// 若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//如果使用設計的組件,請取消注釋以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
}
========返回的responseText========
復制代碼 代碼如下:
?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>
結束!
您可能感興趣的文章:- php的webservice的wsdl的XML無法顯示問題的解決方法
- 關于jquery ajax 調用帶參數(shù)的webservice返回XML數(shù)據(jù)一個小細節(jié)
- 分享XmlHttpRequest調用Webservice的一點心得
- Jquery Ajax學習實例6 向WebService發(fā)出請求,返回DataSet(XML) 異步調用
- WebService傳XML 簡單實例