欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Javascript调用Webservice的多种方法

程序员文章站 2022-04-12 12:29:57
复制代码 代码如下:using system; using system.web; using system.web.services; using system.web....
复制代码 代码如下:

using system;
using system.web;
using system.web.services;
using system.web.services.protocols;
[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
public class service : system.web.services.webservice
{
public service ()
{
//uncomment the following line if using designed components
//initializecomponent();
}
[webmethod]
public string sayhelloto(string name)
{
return "hello "+name;
}
}

还是俗了点。:)
2. js调用webservice+xmlhttp的实现部分。
复制代码 代码如下:

<html>
<title>call webservice with javascript and xmlhttp.</title>
<body>
<script language="javascript"><!--


//test function with get method.
function requestbyget(data){
var xmlhttp = new activexobject("microsoft.xmlhttp");
//webservice location.
var url="http://localhost:1323/website6/service.asmx/sayhelloto?name=zach";
xmlhttp.open("get",url, false);
xmlhttp.setrequestheader ("content-type","text/xml; charset=utf-8");
xmlhttp.setrequestheader ("soapaction","http://tempuri.org/sayhelloto");
xmlhttp.send(data);
var result = xmlhttp.status;
//ok
if(result==200) {
document.write(xmlhttp.responsetext);
}
xmlhttp = null;
}

//test function with post method
function requestbypost(value)
{
var data;
data = '<?xml version="1.0" encoding="utf-8"?>';
datadata = 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/">';
datadata = data + '<soap:body>';
datadata = data + '<sayhelloto xmlns="http://tempuri.org/">';
datadata = data + '<name>'+value+'</name>';
datadata = data + '</sayhelloto>';
datadata = data + '</soap:body>';
datadata = data + '</soap:envelope>';

var xmlhttp = new activexobject("microsoft.xmlhttp");
var url="http://localhost:1323/website6/service.asmx";
xmlhttp.open("post",url, false);
xmlhttp.setrequestheader ("content-type","text/xml; charset=gb2312");
xmlhttp.setrequestheader ("soapaction","http://tempuri.org/sayhelloto");
xmlhttp.send(data);
document.write( xmlhttp.responsetext);
}


// --></script>

<input type="button" value="callwebservicebyget" onclick="requestbyget(null)">
<input type="button" value="callwebservicebypost" onclick="requestbypost('zach')">
</body>
</html>

对于使用post方法需要发送的那堆东东可以在webservice的测试页面中找到,自己拼凑加上对应的参数就可以。
通过style.behavior来实现的方法(比较简单)
function getfemale()
{
//第一个参数是webservice的url,后面是名称
female.useservice("news.asmx?wsdl","news");
//设置一个回调函数,service返回结果的时候回调;第一个参数是回调函数的名称,后面的是webservice的参数
intcallid=female.news.callservice(female_result,"getphoto","female"); //这里有两个参数.....
}
function female_result(result)//回调函数
{
if(result.error)
{
female.innerhtml=result.errordetail.string;
}
else
{
female.innerhtml=result.value; //将webservice返回的结果写如div中
}
}
页面显示部分: <div id="female" style="behavior:url(webservice.htc)"></div>
ok,这给我们在静态页调用动态的内容提供了一种途径;
这里如果给getfemale()函数加上定时调用的话,就是一种无刷新更新页面的机制了。
缺点是webservice会有一定的延迟,即使是本地的webservice也会比静态页面慢很多,初次打开页面会感觉很不协调。
第二种方法使用了style.代码就简洁多了他使用了css.定义了div的行为.比起第一种方法,就易读多了:)
style="behavior:url(webservice.htc)"
前提条件是:
if you are using microsoft ie 5 or later, you can use the behavior/html-component "webservice" to access a web service. the "webservice" behavior communicates with web services over http using simple object access protocol (soap).
附注:另一个总结帖子在:http://goody9807.cnblogs.com/archive/2005/08/17/216725.html
calling webservices using javascript
if you are using microsoft ie 5 or later, you can use the behavior/html-component "webservice" to access a web service. the "webservice" behavior communicates with web services over http using simple object access protocol (soap).
to use the "webservice" behavior, you must attach it to an element using the style attribute, as follows:
style="behavior:url(webservice.htc)">
附上ibm上面有关ajax调用webservice的文章: 
 使用 ajax 调用 soap web 服务,第 1 部分: 构建 web 服务客户机