異步調(diào)用webservice返回responseXML為空的問(wèn)題解決方法
1)要熟悉javascript對(duì)XML文件的加載與操作;
DOM的XML操作可參考的示例:http://www.w3school.com.cn/xmldom/met_document_getelementsbytagname.asp
2)在IE下面還是要通過(guò)loadXML來(lái)轉(zhuǎn)responseText;
3)xml加載后異步屬性設(shè)置;
4)命名空間處理等問(wèn)題;
下面上代碼:
========ASPX前臺(tái)代碼========
<%@ 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() {
//這是我們?cè)诘谝徊街袆?chuàng)建的Web服務(wù)的地址
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)建異步對(duì)象
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); // 這個(gè)有
//var xmlDoc = xmlHttp.responseXML; // 這個(gè)是空的,但下面會(huì)讓它出來(lái)
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周時(shí)間!
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>
========后臺(tái)CS文件========
其實(shí)這段沒(méi)有實(shí)質(zhì)內(nèi)容
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 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//如果使用設(shè)計(jì)的組件,請(qǐng)取消注釋以下行
//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>
結(jié)束!
相關(guān)文章
ASP.NET中實(shí)現(xiàn)文件的保護(hù)性下載基礎(chǔ)篇
許多時(shí)候,我們需要在因特網(wǎng)上提供文件下載服務(wù),但是又要防止未經(jīng)授權(quán)的下載,這時(shí)該怎么辦?本文將為讀者詳細(xì)介紹一種使用ASP.NET實(shí)現(xiàn)的HTTP處理程序的解決方案。2011-02-02ASP.NET Core文件上傳與下載實(shí)例(多種上傳方式)
下面小編就為大家分享一篇ASP.NET Core文件上傳與下載實(shí)例(多種上傳方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01ASP.NET實(shí)現(xiàn)頁(yè)面?zhèn)髦档膸追N方法小結(jié)
這篇文章介紹了ASP.NET實(shí)現(xiàn)頁(yè)面?zhèn)髦档膸追N方法,有需要的朋友可以參考一下2013-11-11ASP.NET對(duì)HTML頁(yè)面元素進(jìn)行權(quán)限控制(三)
界面每個(gè)元素的權(quán)限也是需要控制的。比如一個(gè)查詢(xún)用戶(hù)的界面里面有查詢(xún)用戶(hù)按鈕,添加用戶(hù)按鈕,刪除用戶(hù)按鈕,不同的角色我們得分配不同的權(quán)限2013-12-12ASP.NET Core對(duì)Controller進(jìn)行單元測(cè)試的完整步驟
這篇文章主要給大家介紹了關(guān)于ASP.NET Core對(duì)Controller進(jìn)行單元測(cè)試的完整步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Asp.Mvc?2.0實(shí)現(xiàn)用戶(hù)注冊(cè)實(shí)例講解(1)
這篇文章主要介紹了Asp.Mvc?2.0如何實(shí)現(xiàn)用戶(hù)注冊(cè),實(shí)例講解很細(xì)致,注冊(cè)功能是每個(gè)網(wǎng)站必不可少的組成部分,感興趣的的朋友可以參考下2015-08-08ADO.Net 類(lèi)型化DataSet的簡(jiǎn)單介紹
今天學(xué)習(xí)了類(lèi)型化DataSet,筆記整理如下,需要的朋友可以參考一下2013-04-04asp.net創(chuàng)建位圖生成驗(yàn)證圖片類(lèi)(驗(yàn)證碼類(lèi))
本文提供一個(gè)asp.net生成驗(yàn)證圖片的類(lèi),功能是顯示簡(jiǎn)單的字符串,大家參考使用吧2014-01-01