js调用webservice的问题~~~~

amingo 2009-09-08 08:07:03
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方式是如何调用?

一般帖子,请勿使用“版主进”之类的字眼,谢谢合作
by 竹子版主
...全文
515 35 打赏 收藏 转发到动态 举报
写回复
用AI写文章
35 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxuyu1118 2010-09-09
  • 打赏
  • 举报
回复
mark mark
amingo 2009-09-09
  • 打赏
  • 举报
回复
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
这句什么意思?

那么SOAP方式是什么样的?
zyug 2009-09-09
  • 打赏
  • 举报
回复
你分别注意post与soap方式的中的
header
url

发送的内容,也就是content
的区别
相信你熟悉之后就能掌握
zyug 2009-09-09
  • 打赏
  • 举报
回复
把上面示例中的用soap调用示例也发给你看一下吧,好有个对照


function SoapWebService()
{
var t = new CreateXmlHttp();
t.URL = "WebService.asmx";
t.OnPostBack = function(){
if (this.readyState == 4) {
alert(this.responseXML.xml);
}
};
//t.Content = "a="+escape("zj")+"&b="+escape("shizhu");
// t.Headers = { "CONTENT-TYPE": "application/x-www-form-urlencoded", "Content-Length": t.Content.length };
t.Content = '<?xml version="1.0" encoding="utf-8"?><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/"> <soap:Body> <HelloWorld xmlns="http://tempuri.org/"> <a>zj</a> <b>shizhu</b> </HelloWorld> </soap:Body></soap:Envelope>';
t.Headers = { "SOAPAction":"http://tempuri.org/HelloWorld", "Content-Type":"text/xml; charset=utf-8","Content-Length": t.Content.length };
t.Send();
}

CopperBell 2009-09-09
  • 打赏
  • 举报
回复
学习了
悔说话的哑巴 2009-09-09
  • 打赏
  • 举报
回复
11楼可以了吧
zyug 2009-09-09
  • 打赏
  • 举报
回复
给几个示例你看一下,都是可用的

SOAP调用

function Record()
{
var url = "/WebService.asmx" ;
var uri = "http://www.netufi.cn" ;
var xmlhttp ;
if( window.attachEvent)
{
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
else
{
xmlhttp = new XMLHttpRequest();
}
if(xmlhttp == null)return;

var soapaction = uri + "/" + 'RecordVisited' ;

xmlhttp.open("POST",url,true) ;

xmlhttp.setRequestHeader("SOAPAction", soapaction) ;

xmlhttp.setRequestHeader("Content-Type","text/xml") ;
var content = '<?xml version="1.0" encoding="utf-8"?> <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/"> <soap:Body> <RecordVisited xmlns="http://www.netufi.cn/"> <url>'+window.location+'</url> <reffer>'+escape(document.referrer)+'</reffer> </RecordVisited> </soap:Body></soap:Envelope> ';
xmlhttp.send (content) ;





Post调用完整示例
因为后我封装了js类顺便一起发给你

webservice.asmx

using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

/// <summary>
///WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {

public WebService () {

//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}

[WebMethod]
public string HelloWorld(string a,string b) {
return string.Format("{0}{1}", a, b);
}

}


Default.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>


<script>

function CreateXmlHttp() {
this.XMLHTTP = window.attachEvent ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
this.Method = "POST";
this.URL = "/common/guestbookentity.asp";
this.Content = null;
this.Headers = {};
this.OnPostBack = function() { };
this.Send = function() {
if (this.XMLHTTP == null) {
alert("浏览器不支持动态创建异步请求对像");
return;
};
this.XMLHTTP.onreadystatechange = function(o) { return function() { if (o.OnPostBack) o.OnPostBack.apply(o.XMLHTTP, [o]); }; } (this);
this.XMLHTTP.open(this.Method, this.URL, true);
if (this.Headers) {
for (var property in this.Headers) {
this.XMLHTTP.setRequestHeader(property, this.Headers[property]);
}
}
this.XMLHTTP.send(this.Content);
};
};




function PostWebService()
{
var t = new CreateXmlHttp();
t.URL = "WebService.asmx/HelloWorld";
t.OnPostBack = function(){
if (this.readyState == 4) {
alert(this.responseText);
}
};
t.Content = "a="+escape("zj")+"&b="+escape("shizhu");
t.Headers = { "CONTENT-TYPE": "application/x-www-form-urlencoded", "Content-Length": t.Content.length };
t.Send();
}
</script>
</head>
<body onload="PostWebService()">
<form id="form1" runat="server">
<div>

</div>
</form>
</body>
</html>






个人认为使用post方式比soap方式简单
zyug 2009-09-09
  • 打赏
  • 举报
回复
1.你的这种方式是Post

2.如何确定

xmlhttp.SetRequestHeader ("SOAPAction",Namespace+method)
你应该看你的webservice怎么写的

Namespace对应
[WebService(Namespace = "http://tempuri.org/")]


method对应你调用方法的名称

[WebMethod]
public string HelloWorld() {
return "Hello World";
}


如上面的代码你应该这样写

xmlhttp.SetRequestHeader ("SOAPAction","http://tempuri.org/HelloWorld");

3.如何使用Post方式

var request = getXMLHTTPRequest();
request.open("post", "http://localhost/webservice1/service1.asmx/HelloWorld ", false);
request.setRequestheader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestheader("Content-length", data.length);
var postcontet = "name="+escape("123");
request.send(request)

/*
如何定义post参数
假设我有如下webservice 方法
public sting abc(string a,string b)
{
return "abc";
}
你调用它的参数你的应该组合成 "a=参数a&b=参数b" 的形式,代码如下
var postcontent = "a="+escape("abc")+"&b="+escape("bcd");
*/



3,重点soap与post的区别
1 .soap方式要指定soapaction的header(主要传递调用的方法),传输内容应该是xml的参数调用形式(主要传递调用参数)

2 .post方式 必须要指定
request.setRequestheader("Content-Type", "application/x-www-form-urlencoded");
指明解析传内容的方式
URL中指定要调用的方法
request.open("post", "http://localhost/webservice1/service1.asmx/HelloWorld ", false);
send的内容中指定调用的参数
var postcontet = "name="+escape("123");



flyerwing 2009-09-09
  • 打赏
  • 举报
回复
up!
txg92 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 chen_ya_ping 的回复:]
其实楼主用微软的ASP.NET AJAX 客户端编程来实现与webservice的连接还是很方便的。楼主可以考虑试一下。
[/Quote]

是的,建议用微软的ASP.NET AJAX,它里面用JS调用webservice很简单,而且代码很容易看懂,楼主的AJAX代码我看不懂,帮顶了
chen_ya_ping 2009-09-09
  • 打赏
  • 举报
回复
其实楼主用微软的ASP.NET AJAX 客户端编程来实现与webservice的连接还是很方便的。楼主可以考虑试一下。
SK_Aqi 2009-09-09
  • 打赏
  • 举报
回复
帮顶了!!
amingo 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
[System.Web.Services.Protocols.SoapHeaderAttribute( "AuthenticationInfo ")]

xmlhttp.onreadystatechange=handleStateChange;
xmlhttp.Open("POST",url, true);
xmlhttp.SetRequestHeader ("Content-Type","text/xml; charset=utf-8");
xmlhttp.SetRequestHeader ("Content-Length",getlen(data));
xmlhttp.SetRequestHeader ("SOAPAction",_Namespace+method);
xmlhttp.Send(data);


[/Quote]

还是不行!

xmlhttp.SetRequestHeader ("SOAPAction",_Namespace+method);这句具体在我的代码应该怎么写?
amingo 2009-09-09
  • 打赏
  • 举报
回复
[Quote=引用 31 楼 puzhichen 的回复:]
顶!
额滴神啊,竟然看到了传说中的"孟子E章"
[/Quote]

他算老几,你看他发的那些东西 能用吗
amingo 2009-09-09
  • 打赏
  • 举报
回复
回答我的问题就行了,我看了,也搞不定
  • 打赏
  • 举报
回复
学习了
puzhichen 2009-09-09
  • 打赏
  • 举报
回复
顶!
额滴神啊,竟然看到了传说中的"孟子E章"
阿非 2009-09-09
  • 打赏
  • 举报
回复
http://localhost/webservice1/service1.asmx?op=HelloWorld

会将 webservice 所支持的方法调用 全部 显示

可以 看到 如何应用支持的方法 调用 webservice
阿非 2009-09-09
  • 打赏
  • 举报
回复
在你的浏览器中输入

http://localhost/webservice1/service1.asmx?op=HelloWorld

然后查看 soap1.1 中 是如何定义的

加载更多回复(15)

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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