对于C#如何调用webservice也许很多人知道,但是Js调用可能需要仔细研究了。本人前几天因为项目原因,做了一个这样小小的程序,现已调试成功,特分享给大家。
这是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 type="text/javascript" language="javascript">
var xmlhttp;
var value=new Array();
var variable=new Array();
function handleStateChange()
{
if(xmlhttp.readyState==4)
{
if(xmlhttp.status==200)
{
document.getElementById("Label1").innerHTML=xmlhttp.responseText;
}
}
}
function createXmlHttp()
{
var activeKey=new Array("MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
if(window.ActiveXObject)
{
for(var i=0;i<activeKey.length;i++)
{
try
{
xmlHttp=new ActiveXObject(activeKey[i]);
if(xmlHttp!=null)
return xmlHttp;
}
catch(error)
{
continue;
}
}
throw new Error("客户端浏览器版本过低,不支持XMLHttpRequest对象,请更新浏览器");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new window.XMLHttpRequest();
}
}
function RequestByPost(method,variable,value,url,_Namespace)
{
xmlhttp=createXmlHttp();
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 + '<'+method+' xmlns="'+_Namespace+'">';
for(var i=0;i<variable.length;i++)
{
data = data + '<'+variable[i]+'>'+value[i]+'</'+variable[i]+'>';
}
data = data + '</'+method+'>';
data = data + '</soap:Body>';
data = data + '</soap:Envelope>';
//alert(url);
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);
//alert(data);
}
function getlen(str)
{
var bytesCount=0;
for (var i = 0; i < str.length; i++)
{
var c = str.charAt(i);
if (/^[\u0000-\u00ff]$/.test(c))
{
bytesCount += 1;
}
else
{
bytesCount += 2;
}
}
return bytesCount;
}
function Hello_onclick()
{
var currenturl=document.URL;
var endheads=currenturl.substring(7,currenturl.length);
var endhead=endheads.split('/');
var httphead=currenturl.substring(0,7);
var currenthttphead=httphead+endhead[0]+"/WebSite5/WebService/WebService.asmx";
RequestByPost("HelloWorld",new Array("name"),new Array(document.getElementById('Name').value),currenthttphead,"http://tempuri.org/");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="button" type="button" value="点击" οnclick="Hello_onclick();" />
<input id="Name" type="text" value="" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
在webservice中代码如下:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {
public WebService () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld(string name) {
return "Hello World " + name;
}
}
这里面说明一下原理,其间在JS里用的AJAX原理,向webservice发出一个SOAP的请求,webservice将处理的数据以一个xml字符串的格式返回给客户端,这个字符串中就封装了这个处理的结果。其它也没有什么好多说的,有一个地方需要注意,_Namespace这个参数就是webservice 中[WebService(Namespace = "http://tempuri.org/")]这个特性中的字符串。