异步调用webservice返回responseXML为空的问题解决方法

所属分类: 网络编程 / ASP.NET 阅读数: 923
收藏 0 赞 0 分享
先总结几个要领
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() {
//这是我们在第一步中创建的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>';
//创建异步对象
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>

结束!
更多精彩内容其他人还在看

运行page页面时的事件执行顺序及页面的回发与否深度了解

page页面时的事件执行顺序的了解对于一些.net开发者起到者尤关重要的作用;页面的回发与否会涉及到某些事件执行与不执行,在本文中会详细介绍,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

Web.config(应用程序的配置信息)总结

Web.config文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中,接下来详细介绍一下配置情况,感兴趣的朋友可以了解下
收藏 0 赞 0 分享

C#时间格式化(Datetime)用法详解

C#时间格式化Datetime.ToString参数format格式详细用法,本文将进行介绍,感兴趣的朋友可以了解下啊
收藏 0 赞 0 分享

实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上

一直想实现onmouseover和onmouseout应用于RadioButtonList或CheckBoxList控件上。此功能就是当鼠标经过时RadioButtonList或CheckBoxList每一个Item时,让Item有特效显示,离开时,恢复原样有演示动画,感兴趣的朋
收藏 0 赞 0 分享

Asp.net 图片文件防盗链(尊重劳动成果)及BeginRequest事件学习

关于图片盗链这个问题,毕竟是自己的劳动成功,很多人不希望别人就那么轻易地偷走了;反盗链的程序其实很简单,熟悉ASP.NET 应用程序生命周期的话很容易就可以写一个,运用HttpModule在BeginRequest事件中拦截请求就ok了
收藏 0 赞 0 分享

asp.net Grid 导出Excel实现程序代码

看了FineUI中的将Grid导出为Excel的实现方法,实际上是可以非常简单。看来很难的问题,变换一种思路就可以非常简单
收藏 0 赞 0 分享

使用C#处理WebBrowser控件在不同域名中的跨域问题

我们在做web测试时,经常会使用WebBrowser来进行一些自动化的任务而有些网页上面会用IFrame去嵌套别的页面,这些页面可能不是在相同域名下的,这时就会出现跨域问题,无法直接在WebBrowser中获取到IFrame中的元素,接下来介绍如何解决此问题,需要了解的朋友可以参
收藏 0 赞 0 分享

.NET 下运用策略模式(组合行为和实体的一种模式)

我简单的理解策略模式就是把行为(方法)单独的抽象出来,并采用组合(Has-a)的方式,来组合行为和实体的一种模式比如,.NET中对数组排序的Sort的方法就是一个策略模式的实现模板
收藏 0 赞 0 分享

使用Entity Framework(4.3.1版本)遇到的问题整理

在这里记录一下之前使用Entity Framework(4.3.1版本)遇到的问题:更新没有设置主键的表、更改Code-First的默认连接、检测字符串截断错误,需要的朋友可以参考下
收藏 0 赞 0 分享

Asp.net利用JQuery AJAX实现无刷新评论思路与代码

Asp.net利用JQuery AJAX实现无刷新评论,此功能是每一个从事asp.net开发者的朋友都希望实现的,本文利用闲暇时间整理了一些,有需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多