Javascript调用Webservice的多种方法

所属分类: 网络编程 / ASP.NET 阅读数: 1364
收藏 0 赞 0 分享
复制代码 代码如下:

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 服务客户机
更多精彩内容其他人还在看

ASP.NET比较常用的26个性能优化技巧

这篇文章主要给大家介绍asp.net中比较常用的26个性能优化技巧,主要设计到asp.net中常用的26个性能优化方面的内容,对于asp.net中常用的26个性能优化技巧感兴趣的朋友可以参考下本篇文章
收藏 0 赞 0 分享

区分ASP.NET中get方法和post方法

我们都知道,get是从服务器上获取数据,post是向服务器上传数据。本文主要介绍ASP.NET中get方法和post方法的区别,需要的朋友可以参考下
收藏 0 赞 0 分享

SQL Server数据库连接 Web.config如何配置

以下的文章主要描述的是Web.config正确配置SQL Server数据库连接的实际擦步骤。我们以图文结合的方式对其有个更好的说明,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP.NET MVC5实现文件上传与地址变化处理(5)

这篇文章主要介绍了ASP.NET MVC5实现文件上传与地址变化处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ASP.NET MVC5网站开发概述(一)

这篇文章主要内容是ASP.NET MVC5网站开发实践的整体概述,分析了开发环境、使用的技术以及项目的整体结构,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

asp.net中GridView数据鼠标移入显示提示信息

本篇文章给大家介绍在asp.net中gridview数据鼠标移入显示提示信息,需要的朋友可以参考下本文
收藏 0 赞 0 分享

一个简单MVC5 + EF6示例分享

本文小编跟大家分享了一个简单MVC5 + EF6示例,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ASP.NET MVC5网站开发项目框架(二)

这篇文章主要介绍了ASP.NET MVC5网站开发项目框架,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

ASP.NET MVC5 网站开发框架模型、数据存储、业务逻辑(三)

上次搭建好了项目框架,但还是觉得不太对劲,后来才想起来没有对开发目标进行定位,这个小demo虽然不用做需求分析,但是要实现什么效果还得明确。后来想了一下就做个最简单的网站,目标定为小公司进行展示用的网站。功能有显示用的文章功能,咨询留言,评论等
收藏 0 赞 0 分享

ASP.NET MVC 5之邮件服务器与客户端

这篇文章主要介绍了ASP.NET MVC5邮件服务器与客户端,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多