Javascript调用Webservice的多种方法

时间:2021-05-26

复制代码 代码如下:
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://municates 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 服务客户机

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章