Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script src= "Js/jquery1.7.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
$ (function () {
$ (' #Button1 '). Click (function () {
var username = $ (' #txtUserName '). Val ();
var pwd = $ (' #txtPwd '). Val ();
$.ajax ({
Type: "Post",
ContentType: "Application/json",
URL: "Webservice1.asmx/login",
Data: "{username: '" + username + "', pwd: '" + pwd + "'}",
Success:function (bukeyi) {
if (Bukeyi.d = = ' true ') {
window.location = ' htmlpage1.htm ';
}
else {
$ (' #divinfo '). Text ("User name or password error");
}
}
})
})
})
</script>
<body>
User name <input id= "txtUserName" type= "text"/><br/>
Password <input id= "txtpwd" type= "text"/><br/>
<input id= "Button1" type= "button" value= "Login"/><br/>
<div id= "Divinfo" ></div>
</body>
------webservice1.asmx----
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Services;
Namespace ajax11
{
<summary>
Summary description of WebService1
</summary>
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[System.ComponentModel.ToolboxItem (False)]
To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.
[System.Web.Script.Services.ScriptService]
public class WebService1:System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld ()
{
Return to "Hello World";
}
[WebMethod]
public string ValidateUser (string username)
{
if (username = = "Onlifes")
{
Return "User name has been occupied, please select another";
}
Else
{
Return "can be used, please continue";
}
}
[WebMethod]
public string GetDate ()
{
Return DateTime.Now.ToString ("Yyyy-mm-dd hh:mm:ss");
}
[WebMethod]
public string Login (string username, string pwd)
{
if (username = "admin" && pwd = = "888888")
{
Return "true";
}
Else
{return ' false ';}
}
}
}