net framework 在JavaScript中调用ASP.NET WebService的简单方法
在JavaScript中调用ASP.NET WebService的简单方法
客户端 JavaScript 调用 ASP NET WebService 的方法除了采用 WebServer htc 和 构造 SOAPAction 的方法外 下面介绍一个采用 Ajax 调用的简单方法 并且可以传递参数 其实 ASP NET WebService 就是一个网站 所以 Request 对象是可用的 这样 传递参数就很容易了 下面是一个WebService asmx的代码
ASMX 代码
<%@ WebService Language= C# CodeBehind= WebService asmx cs Class= WebService %>
C# 代码
using System;
using System Collections Generic;
using System Linq;
using System Web;
using System Web Services;
using System Data;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = )]
[WebServiceBinding(ConformsTo = WsiProfiles BasicProfile _ )]
[System ComponentModel ToolboxItem(false)]
// To allow this Web Service to be called from script using ASP NET AJAX unment the following line
// [System Web Script Services ScriptService]
public class WebService : System Web Services WebService
{
[WebMethod]
// 字符串返回测试
public string GetServerTime()
{
return 当前服务器时间 + DateTime Now ToString();
}
[WebMethod]
// long 类型返回测试
public long GetServerTimeTicks()
{
return DateTime Now Ticks;
}
[WebMethod]
// Datatable返回测试
public DataTable GetTestDataTable()
{
DataTable dt = new DataTable( TestTable );
DataRow dr;
dt Columns Add(new DataColumn( id typeof(Int )));
dt Columns Add(new DataColumn( text typeof(string)));
for (int i = ; i < ; i++)
{
dr = dt NewRow();
dr[ ] = i;
dr[ ] = 列表项目 + i ToString();
dt Rows Add(dr);
}
return dt;
}
[WebMethod]
// List 类型测试
public List<User> GetTestUser()
{
//传递参数传测试
string param = this Context Request QueryString[ param ];
if (param == null) param= this Context Request Form[ param ];
List<User> u_list = new List<User>();
for (int i = ; i < ; i++)
{
User u = new User();
u Name = LoginName + i ToString() + param = + param;
u Title = 孟宪会 + i ToString();
u_list Add(u);
}
return u_list;
}
//定义一个对象 User
public class User
{
public String Name { get; set; }
public String Title { get; set; }
}
}
客户端调用的代码
HTML 代码
<!DOCTYPE PUBLIC //W C//DTD XHTML Transitional//EN transitional dtd >
< xmlns= >
<head runat= server >
<title>JavaScript 调用 ASP NET Web 服务测试</title>
<script type= text/javascript >
var xmlHttp = null;
function createXMLHttpRequest() {
try {
if (window XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
else if (window ActiveXObject)
xmlHttp = new ActiveXObject( Microsoft XMLHTTP );
}
catch (ex) { }
}
function AsynRequest() {
createXMLHttpRequest();
if (xmlHttp == null) {
alert( 不能创建 XmlHttpRequest 对象 );
return;
}
xmlHttp open( GET WebService asmx/GetTestUser?param=net_lover true);
xmlHttp setRequestHeader( Connection close );
xmlHttp onreadystatechange = function () {
if (xmlHttp readyState == ) {
if (xmlHttp status == ) {
var userList = xmlHttp responseXML getElementsByTagName( User );

for (i = ; i < userList length; i++) {
document getElementById( get ) innerHTML += userList[i] getElementsByTagName( Name )[ ] firstChild nodeValue + ;
document getElementById( get ) innerHTML += userList[i] getElementsByTagName( Title )[ ] firstChild nodeValue + <br/> ;
}
}
}
};
xmlHttp send();
}
function AsynPostRequest() {
createXMLHttpRequest();
if (xmlHttp == null) {
alert( 不能创建 XmlHttpRequest 对象 );
return;
}
var data = param =abc ;
xmlHttp open( POST WebService asmx/GetTestUser?t= + Date parse(new Date()) true);
xmlHttp setRequestHeader( Content type application/x form urlencoded );
xmlHttp setRequestHeader( Content length data length);
xmlHttp setRequestHeader( Connection close );
xmlHttp onreadystatechange = function () {
if (xmlHttp readyState == ) {
if (xmlHttp status == ) {
var userList = xmlHttp responseXML getElementsByTagName( User );
for (i = ; i < userList length; i++) {
document getElementById( post ) innerHTML += userList[i] getElementsByTagName( Name )[ ] firstChild nodeValue + ;
document getElementById( post ) innerHTML += userList[i] getElementsByTagName( Title )[ ] firstChild nodeValue + <br/> ;
}
}
}
};
xmlHttp send(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>
</>
需要注意的是 使用此方法需要在nfig里加入以下的配置
nfig 代码
<system web>
<webServices>
<protocols>
<add name = HttpPost />
<add name = HttpGet />
</protocols>
</webServices>
lishixinzhi/Article/program/net/201311/12604相关文章
- NETframework ASP.NET 2.0 中实现跨页提交
- aspnet项目开发教程 ASP.NET入门教程 12.2.2 传送Web服务[1]
- java自学宝典怎么样 ASP.NET开发宝典:运行ASP.NET网站
- 完善运行机制 ASP.NET开发宝典:.NET Web运行机制
- net开发框架 ASP.NET开发宝典:.NET Framework介绍
- net开发框架 ASP.NET开发宝典
- NET开发 ASP.NET中的HTTP模块和处理程序(上)
- net开发框架有哪些 基于ASP.NET MVC框架开发Web论坛应用程序[3]
- ps应用程序框架 基于ASP.NET MVC框架开发Web论坛应用程序[1]
- net框架有两个重要组成部分 ASP.NET MVC 4框架:IIS 7.0与ASP.NET(2)
爱学记

微信收款码
支付宝收款码