页面文件:
ajax.ashx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Threading;
namespace ajaxtext
{
///
/// $codebehindclassname$ 的摘要说明
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ajax : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string uname = string.Empty;
string passwd = string.Empty;
uname = context.Request["u"].ToString();
passwd = context.Request["p"].ToString();
Thread.Sleep(1000);
if (uname == "gen.li" && passwd == "123456")
{
context.Response.Write("success");
}
else
context.Response.Write("fail");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
JS:
function ajax_test() {
var uname = document.getElementById("username").value;
var passwd = document.getElementById("passwd").value;
$.ajax({
type: "POST",
url: "ajax.ashx",
data: "u=" + uname + "&p=" + passwd,
beforeSend: function() {
$("#result").text("数据处理中...");
},
success: function(msg) {
if (msg == "success") {
$("#result").text("验证通过!");
}
else {
$("#result").text("验证失败,密码或者用户名错误!");
}
}
});
}