Html File:
<!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>
<title>Ajax & Web Service</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">...
body {...}{ font-size:12px; }
.testArea {...}{ width:20%;border:solid 1px #DDDDDD;background-color:#F7F7F7; }
.testTable {...}{ width:100%;border:0px;padding:0px; }
.inpButt {...}{ height:20px; }
</style>
<script type="text/javascript">...
// Trim All
String.prototype.Trim = function() 
...{
return this.replace(/(^s*)|(s*$)/g, "");
}
// Trim Left
String.prototype.LTrim = function() 
...{
return this.replace(/(^s*)/g, "");
}
// Trim Right
String.prototype.RTrim = function() 
...{
return this.replace(/(s*$)/g, "");
}
// XmlHttp Object
var ajaxObj = new function()
...{
var obj = false;
if(window.XMLHttpRequest) // Mozila,FireFox 类型浏览器
...{
try
...{
obj = new XMLHttpRequest();
}
catch(e1)
...{
}
}
else if(window.ActiveXObject) // IE 浏览器
...{
try
...{
obj = new ActiveXObject("MSXML2.XMLHTTP"); // New Version
}
catch(e2)
...{
try
...{
obj = new ActiveXObject("Microsoft.XMLHTTP"); // Old Version
}
catch(e3)
...{
}
}
}
else // 其他浏览器
...{
}
return obj;
}
// Check XmlHttp Object
if(!ajaxObj)
...{
window.alert("不支持XmlHttp!");
window.close();
}

/**//*******************************
* Check Form Submit
*******************************/
function checkSum()
...{
var userNameObj = document.getElementById("lUserName");
var userPwdObj = document.getElementById("lUserPwd");
if(userNameObj && userPwdObj)
...{
var userNameVal = userNameObj.value.Trim();
var userPwdVal = userPwdObj.value.Trim();
if(userNameObj.value.Trim() != "" && userPwdObj.value.Trim() != "" )
...{
ajaxLogin(userNameVal,userPwdVal);
}
else
...{
window.alert("“用户名”和“密码”为必填项,请填写完整!");
userNameObj.value = userNameObj.value.Trim();
userPwdObj.value = userPwdObj.value.Trim();
}
}
}

/**//*******************************
* Do Login Action
*******************************/
function ajaxLogin(uName,uPwd)
...{
ajaxObj.onreadystatechange = function()
...{
if(ajaxObj.readyState == 4)
...{
var mylevl = ajaxObj.responseText;
document.getElementById("resultDiv").innerHTML = mylevl;
ajaxObj.abort(); // 重置状态
}
}
getOpen(ajaxObj,uName,uPwd);
}

/**//**************************************
* POST Method
**************************************/
function postOpen(obj,uName,uPwd)
...{
var url = "http://pgq/SDTWebServices/SDTService.asmx/UserLogin";
var data = "UserName=" + uName + "&UserPassword=" + uPwd;
obj.open("POST",url,false); // Post方式,异步操作
obj.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); // Request头信息
obj.setRequestHeader("Content-Length",data.length);// Request头信息
obj.send(data); // POST参数
}

/**//**************************************
* SOAP 1.1 Method
**************************************/
function soap1Open(obj,uName,uPwd)
...{
var url = "http://pgq/SDTWebServices/SDTService.asmx";
var data = "<?xml version="1.0" encoding="utf-8"?>";
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 += "<soap:Body>";
data += "<UserLogin xmlns="http://pgq/SDTWebServices/">";
data += "<UserName>"+ uName +"</UserName>";
data += "<UserPassword>"+ uPwd +"</UserPassword>";
data += "</UserLogin>";
data += "</soap:Body>";
data += "</soap:Envelope>";
obj.open("POST",url,false);
obj.setRequestHeader("Content-Type","text/xml; charset=utf-8");
obj.setRequestHeader("Content-Length",data.length);
obj.setRequestHeader("SOAPAction","http://pgq/SDTWebServices/UserLogin");
obj.send(data);
}

/**//**************************************
* SOAP 1.2 Method
**************************************/
function soap2Open(obj,uName,uPwd)
...{
var url = "http://pgq/SDTWebServices/SDTService.asmx";
var data = "<?xml version="1.0" encoding="utf-8"?>";
data += "<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">";
data += "<soap12:Body>";
data += "<UserLogin xmlns="http://pgq/SDTWebServices/">";
data += "<UserName>"+ uName +"</UserName>";
data += "<UserPassword>"+ uPwd +"</UserPassword>";
data += "</UserLogin>";
data += "</soap12:Body>";
data += "</soap12:Envelope>";
obj.open("POST",url,false);
obj.setRequestHeader("Content-Type","application/soap+xml; charset=utf-8");
obj.setRequestHeader("Content-Length",data.length);
obj.send(data);
}

/**//**************************************
* GET Method
**************************************/
function getOpen(obj,uName,uPwd)
...{
var url = "http://pgq/SDTWebServices/SDTService.asmx/UserLogin?UserName=" + uName + "&UserPassword=" + uPwd;
var data = "";
obj.open("GET",url,false);
obj.setRequestHeader("Content-Type","text/xml; charset=utf-8");
obj.setRequestHeader("Content-Length","10240");
obj.send(data);
}
</script>
</head>
<body>
<form id="TestForm">
<div id="UserLoginMethodTestArea" class="testArea">
<p style="text-align:center;">User Login Method Test</p>
<table class="testTable">
<tr>
<td>用户名:</td>
<td><input id="lUserName" type="text" style="width:100px;"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input id="lUserPwd" type="password" style="width:100px;"/></td>
</tr>
<tr>
<td colspan="2" style="text-align:center;">
<input id="lSub" type="button" value="登陆" onclick="javascript:checkSum();" class="inpButt"/> <input id="lReset" type="reset" value="取消" class="inpButt"/>
</td>
</tr>
</table>
</div>
<div id="resultDiv"></div>
</form>
</body>
</html>ASMX C# File:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
namespace SDT
...{
[WebService(Name = "SDT Team Web Services", Namespace = "http://pgq/SDTWebServices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class SDTService : WebService
...{
public SDTService()
...{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
UserLoginDBConnString = ConfigurationManager.ConnectionStrings["UserLoginMethodDBConnString"].ToString();
UserLoginDBConn = new SqlConnection(UserLoginDBConnString);
ExceptionString = "<?xml version="1.0" encoding="utf-8"?><exception>{0}</exception>";
}
public string UserLoginDBConnString;
public SqlConnection UserLoginDBConn;
public string ExceptionString;
[WebMethod(Description = "User Login Method",MessageName = "UserLogin")]
public string UserLogin(string UserName,string UserPassword)
...{
try
...{
UserLoginDBConn.Open();
string strSql = "select * from UserInfo where UserName='" + UserName + "' and Password='" + UserPassword + "'";
SqlDataAdapter myAdapter = new SqlDataAdapter(strSql, UserLoginDBConn);
DataSet UserDS = new DataSet();
myAdapter.Fill(UserDS);
if(UserDS.Tables[0].Rows.Count > 0)
...{
return UserDS.GetXml();
}
else
...{
return "0";
}
}
catch(Exception e)
...{
return e.Message;
}
finally
...{
UserLoginDBConn.Close();
}
}
}
}
ASMX File:
<%@ WebService Language="C#" CodeBehind="~/App_Code/SDTService.cs" Class="SDT.SDTService" %>Web.Config File:
<?xml version="1.0" encoding="utf-8"?>
<!--
注意: 除了手动编辑此文件以外,您还可以使用
Web 管理工具来配置应用程序的设置。可以使用 Visual Studio 中的
“网站”->“Asp.Net 配置”选项。
设置和注释的完整列表在
machine.config.comments 中,该文件通常位于
WindowsMicrosoft.NetFrameworkv2.xConfig 中
-->
<configuration>
<appSettings/>
<connectionStrings>
<add name="UserLoginMethodDBConnString" connectionString="Server=PGQMSSQL2K5;Database=SDT_DB;User ID=sa;Password=wearefutures;" providerName="System.Data.SqlClient"/>
<!--<add name="UserLoginMethodDBConnString" connectionString="Server=PGQMSSQL2K5;Database=SDT_DB;User ID=sa;Password=wearefutures;Trusted_Connection=true;" providerName="System.Data.SqlClient"/>-->
</connectionStrings>
<system.web>
<!--
设置 compilation debug="true" 将调试符号插入
已编译的页面中。但由于这会
影响性能,因此只在开发过程中将此值
设置为 true。
-->
<compilation debug="true"/>
<!--
通过 <authentication> 节可以配置 ASP.NET 使用的
安全身份验证模式,
以标识传入的用户。
-->
<authentication mode="Windows"/>
<!--
如果在执行请求的过程中出现未处理的错误,
则通过 <customErrors> 节可以配置相应的处理步骤。具体说来,
开发人员通过该节可以配置
要显示的 html 错误页
以代替错误堆栈跟踪。
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
<!--
<webServices>
<protocols>
<add name="HttpPost" />
<add name="HttpGet" />
</protocols>
</webServices>
-->
</system.web>
</configuration>
1301

被折叠的 条评论
为什么被折叠?



