js调用webservice的问题~~~~版主进

amingo 2009-09-08 07:58:51
js:
function getXMLHTTPRequest()
{
var xRequest = null;
if( window.XMLHttpRequest )
{
xRequest = new XMLHttpRequest(); // Mozilla and Safari
}
else if( typeof ActiveXObject != "undefined" )
{
xRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
return xRequest;
}

function getWebService()
{
var request = getXMLHTTPRequest();

request.open("post", "http://localhost/webservice1/service1.asmx", false);

request.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
request.SetRequestHeader ("SOAPAction","http://tempuri.org/HelloWorld");

var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
data = data + '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">';
data = data + '<soap:Body>';
data = data + '<HelloWorld xmlns="http://tempuri.org/">';
data = data + '<name>123</name>';
data = data + '</HelloWorld>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';

request.onreadystatechange=function()
{
if (request.readyState==4)
{
if(request.status ==200)
{
alert( request.responseText);
}
}
}
request.send(data);

}


vb.net:
<WebMethod()> _
Public Function HelloWorld(ByVal name As String) As String
Return name
End Function

<web.config>中也进行了配置

执行getWebService()后报错了,System.Web.Services.Protocols.SoapException: 服务器未能识别 HTTP 标头 SOAPAction 的值:http://tempuri.org/HelloWorld。
at System.Web.Services.Protocols.Soap11ServerProtocolHelper.RouteRequest()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
不知如何解决??

另外这种调用方式是post还是SOAP,我觉得应该是SOAP,那么post方式是如何调用?
...全文
63 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿非 2009-09-09
  • 打赏
  • 举报
回复
利用XMLHttpRequest对象异步发送XML文档
http://blog.csdn.net/Sandy945/archive/2009/05/12/4170512.aspx
amingo 2009-09-09
  • 打赏
  • 举报
回复
阿非 2009-09-09
  • 打赏
  • 举报
回复

<script type="text/javascript">
var xmlHttp=null;

function createXMLHttpRequest()
{
if(xmlHttp == null){
if(window.XMLHttpRequest) {
//Mozilla 浏览器
xmlHttp = new XMLHttpRequest();
}else if(window.ActiveXObject) {
// IE浏览器
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
}
}

function openAjax()
{

if( xmlHttp == null)
{
createXMLHttpRequest();
if( xmlHttp == null)
{
//alert('出错');
return ;
}
}

var strPostSoap = "";
strPostSoap += "<?xml version='1.0' encoding='utf-8'?>";
strPostSoap += "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
strPostSoap += " <soap:Body>";
strPostSoap += " <HelloWorld xmlns='http://tempuri.org/'>";
strPostSoap += " <name>123</name>";// 参数值
strPostSoap += " </HelloWorld>";
strPostSoap += " </soap:Body>";
strPostSoap += "</soap:Envelope>";





xmlHttp.onreadystatechange=xmlHttpChange;

xmlHttp.open("post","http://localhost/webservice1/service1.asmx",true);


xmlHttp.setRequestHeader("Content-Type", "text/xml");
xmlHttp.setRequestHeader("Host", "localhost");
xmlHttp.setRequestHeader("content-length",strPostSoap.length);
xmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/HelloWorld");
//发送SOAP请求
xmlHttp.send(strPostSoap);

}

function xmlHttpChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
alert(xmlHttp.responseText);
}
}
}
</script>
amingo 2009-09-09
  • 打赏
  • 举报
回复
版主进

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧