异步调用webservice返回responseXML为空的问题解决方法
程序员文章站
2024-03-02 17:01:22
先总结几个要领: 1)要熟悉javascript对xml文件的加载与操作; dom的xml操作可参考的示例:http://www.w3school.com.cn/xmldo...
先总结几个要领:
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>
结束!
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>
结束!
上一篇: APIDOC 初次使用记录