用JQUERY在ASP.NET环境(C#)实现最基本的AJAX

来源:互联网 发布:淘宝中差评处理话术 编辑:程序博客网 时间:2024/05/17 20:26
页面文件:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ajaxtext._Default" %>
用户: 密码: 显示结果
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("验证失败,密码或者用户名错误!"); } } }); }
原创粉丝点击