需求:通过输入编号找到对应内容
.ashx可执行文件代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;
using System.Web.Services;
namespace WebApplication1
{
///
/// test 的摘要说明
///
public class test : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = “text/plain”;
string action = context.Request.QueryString[“action”].ToString();
if (action == “bbb”)
{
context.Response.Write(Result(context));
}
}
public bool IsReusable
{
get
{
return false;
}
}
public string Result(HttpContext context)
{
string userName = context.Request.QueryString[“userName”].ToString();
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add(“111”, “第一组数据” + “;”+ “sadfgh”);
list.Add(“222”, “第二组数据”);
list.Add(“333”, “第三组数据” + “;” + “sadfgh”);
list.Add(“444”, “第四组数据”);
var result = from pair in list orderby pair.Key select pair;
foreach (KeyValuePair<string, string> pair in result)
{
if (pair.Key == userName)
{
DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(String));
MemoryStream msObj = new MemoryStream();
//将序列化之后的Json格式数据写入流中
js.WriteObject(msObj, pair.Value);
msObj.Position = 0;
//从0这个位置开始读取流中的数据
StreamReader sr = new StreamReader(msObj, Encoding.UTF8);
string json = sr.ReadToEnd();
sr.Close();
msObj.Close();
return json;
}
}
return "编号输入有误,请重新输入!";
}
}
}
前台代码
<%@ Page Language=“C#” AutoEventWireup=“true” CodeBehind=“LoginGet.aspx.cs” Inherits=“WebApplication1.LoginGet” %>
<script type="text/javascript">
$(function () {
$("#txtName").blur(function () {
JudgeUserName();
});
});
function JudgeUserName() {
$.ajax({
type: "get",
url: "/test.ashx",
//dataType:"text",
data: { "userName": $("#txtName").val(), "action": "bbb" },
success: function (msg) {
// alert(msg);
$("#MsgContent").empty();
$("#MsgContent").append(msg);
}
});
}
</script>