客戶端 JavaScript 調用 ASP
ASMX 代碼
<%@ WebService Language=
C# 代碼
using System;
using System
using System
using System
using System
using System
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace =
[WebServiceBinding(ConformsTo = WsiProfiles
[System
// To allow this Web Service to be called from script
// [System
public class WebService
{
[WebMethod]
// 字符串返回測試
public string GetServerTime()
{
return
}
[WebMethod]
// long 類型返回測試
public long GetServerTimeTicks()
{
return DateTime
}
[WebMethod]
// Datatable返回測試
public DataTable GetTestDataTable()
{
DataTable dt = new DataTable(
DataRow dr;
dt
dt
for (int i =
{
dr = dt
dr[
dr[
dt
}
return dt;
}
[WebMethod]
// List 類型測試
public List<User> GetTestUser()
{
//傳遞參數傳測試
string param = this
if (param == null) param= this
List<User> u_list = new List<User>();
for (int i =
{
User u = new User();
u
u
u_list
}
return u_list;
}
//定義一個對象 User
public class User
{
public String Name { get; set; }
public String Title { get; set; }
}
}
客戶端調用的代碼
HTML 代碼
<!DOCTYPE html PUBLIC
<html xmlns=
<head runat=
<title>JavaScript 調用 ASP
<script type=
var xmlHttp = null;
function createXMLHttpRequest() {
try {
if (window
xmlHttp = new XMLHttpRequest();
else if (window
xmlHttp = new ActiveXObject(
}
catch (ex) { }
}
function AsynRequest() {
createXMLHttpRequest();
if (xmlHttp == null) {
alert(
return;
}
xmlHttp
xmlHttp
xmlHttp
if (xmlHttp
if (xmlHttp
var userList = xmlHttp
for (i =
document
document
}
}
}
};
xmlHttp
}
function AsynPostRequest() {
createXMLHttpRequest();
if (xmlHttp == null) {
alert(
return;
}
var data =
xmlHttp
xmlHttp
xmlHttp
xmlHttp
xmlHttp
if (xmlHttp
if (xmlHttp
var userList = xmlHttp
for (i =
document
document
}
}
}
};
xmlHttp
}
</script>
</head>
<body>
<input type=
<input type=
<div id=
<div id=
</body>
</html>
需要注意的是
nfig 代碼
<system
<webServices>
<protocols>
<add name =
<add name =
</protocols>
</webServices>
</system
From:http://tw.wingwit.com/Article/program/net/201311/12604.html