客户端 JavaScript 调用 ASPNET WebService 的方法除了采用 WebServerhtc 和 构造 SOAPAction 的方法外下面介绍一个采用 Ajax 调用的简单方法并且可以传递参数其实ASPNET WebService 就是一个网站所以Request 对象是可用的这样传递参数就很容易了下面是一个WebServiceasmx的代码
ASMX 代码
<%@ WebService Language=C# CodeBehind=WebServiceasmxcs Class=WebService %>
C# 代码
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemWeb;
using SystemWebServices;
using SystemData;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = )]
[WebServiceBinding(ConformsTo = WsiProfilesBasicProfile_)]
[SystemComponentModelToolboxItem(false)]
// To allow this Web Service to be called from script using ASPNET AJAX uncomment the following line
// [SystemWebScriptServicesScriptService]
public class WebService : SystemWebServicesWebService
{
[WebMethod]
// 字符串返回测试
public string GetServerTime()
{
return 当前服务器时间 + DateTimeNowToString();
}
[WebMethod]
// long 类型返回测试
public long GetServerTimeTicks()
{
return DateTimeNowTicks;
}
[WebMethod]
// Datatable返回测试
public DataTable GetTestDataTable()
{
DataTable dt = new DataTable(TestTable);
DataRow dr;
dtColumnsAdd(new DataColumn(id typeof(Int)));
dtColumnsAdd(new DataColumn(text typeof(string)));
for (int i = ; i < ; i++)
{
dr = dtNewRow();
dr[] = i;
dr[] = 列表项目 + iToString();
dtRowsAdd(dr);
}
return dt;
}
[WebMethod]
// List 类型测试
public List<User> GetTestUser()
{
//传递参数传测试
string param = thisContextRequestQueryString[param];
if (param == null) param= thisContextRequestForm[param];
List<User> u_list = new List<User>();
for (int i = ; i < ; i++)
{
User u = new User();
uName = LoginName + iToString() + param = + param;
uTitle = 孟宪会 + iToString();
u_listAdd(u);
}
return u_list;
}
//定义一个对象 User
public class User
{
public String Name { get; set; }
public String Title { get; set; }
}
}
客户端调用的代码
HTML 代码
<!DOCTYPE html PUBLIC //WC//DTD XHTML Transitional//EN transitionaldtd>
<html xmlns=>
<head runat=server>
<title>JavaScript 调用 ASPNET Web 服务测试</title>
<script type=text/javascript>
var xmlHttp = null;
function createXMLHttpRequest() {
try {
if (windowXMLHttpRequest)
xmlHttp = new XMLHttpRequest();
else if (windowActiveXObject)
xmlHttp = new ActiveXObject(MicrosoftXMLHTTP);
}
catch (ex) { }
}
function AsynRequest() {
createXMLHttpRequest();
if (xmlHttp == null) {
alert(不能创建 XmlHttpRequest 对象);
return;
}
xmlHttpopen(GET WebServiceasmx/GetTestUser?param=net_lover true);
xmlHttpsetRequestHeader(Connection close);
xmlHttponreadystatechange = function () {
if (xmlHttpreadyState == ) {
if (xmlHttpstatus == ) {
var userList = xmlHttpresponseXMLgetElementsByTagName(User);
for (i = ; i < userListlength; i++) {
documentgetElementById(get)innerHTML += userList[i]getElementsByTagName(Name)[]firstChildnodeValue + ;
documentgetElementById(get)innerHTML += userList[i]getElementsByTagName(Title)[]firstChildnodeValue + <br/>;
}
}
}
};
xmlHttpsend();
}
function AsynPostRequest() {
createXMLHttpRequest();
if (xmlHttp == null) {
alert(不能创建 XmlHttpRequest 对象);
return;
}
var data = param=abc;
xmlHttpopen(POST WebServiceasmx/GetTestUser?t= + Dateparse(new Date()) true);
xmlHttpsetRequestHeader(Contenttype application/xwwwformurlencoded);
xmlHttpsetRequestHeader(Contentlength datalength);
xmlHttpsetRequestHeader(Connection close);
xmlHttponreadystatechange = function () {
if (xmlHttpreadyState == ) {
if (xmlHttpstatus == ) {
var userList = xmlHttpresponseXMLgetElementsByTagName(User);
for (i = ; i < userListlength; i++) {
documentgetElementById(post)innerHTML += userList[i]getElementsByTagName(Name)[]firstChildnodeValue + ;
documentgetElementById(post)innerHTML += userList[i]getElementsByTagName(Title)[]firstChildnodeValue + <br/>;
}
}
}
};
xmlHttpsend(data);
}
</script>
</head>
<body>
<input type=button value=GET 方法调用 onclick=AsynRequest() />
<input type=button value=POST方法调用 onclick=AsynPostRequest() />
<div id=get></div>
<div id=post></div>
</body>
</html>
需要注意的是使用此方法需要在nfig里加入以下的配置
nfig 代码
<systemweb>
<webServices>
<protocols>
<add name = HttpPost />
<add name = HttpGet />
</protocols>
</webServices>
</systemweb>