温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何解决异步调用webservice返回responseXML为空的问题

发布时间:2021-08-02 22:05:07 来源:亿速云 阅读:128 作者:chen 栏目:开发技术

本篇内容主要讲解“如何解决异步调用webservice返回responseXML为空的问题”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何解决异步调用webservice返回responseXML为空的问题”吧!

先总结几个要领
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>

结束!

到此,相信大家对“如何解决异步调用webservice返回responseXML为空的问题”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI