1. 程式人生 > >JQuery.AJAX中的post方法

JQuery.AJAX中的post方法

<%@ WebHandler Language="C#" Class="post" %>

using System;
using System.Web;

using System.Data.SqlClient;
using System.Data;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class post : IHttpHandler {

public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "
text/plain";
SqlConnection conn = new SqlConnection("server=.;database=LTArticle;uid=sa;pwd=;");
conn.Open();
String CId = context.Request.Form["CId"];
SqlCommand com = new SqlCommand("select id,Title from LT_Article where ColumnId=" + CId, conn);
SqlDataReader sdr = com.ExecuteReader();

StringWriter sw = new
StringWriter();
JsonWriter writer = new JsonWriter(sw);
writer.WriteStartArray(); // [ while (sdr.Read())
{
writer.WriteStartObject(); // { for (int i = 0; i < sdr.FieldCount; i++)
{
writer.WritePropertyName(sdr.GetName(i).Trim()); //
行的列名 writer.WriteValue(sdr[i].ToString());
}
writer.WriteEndObject(); // } }
writer.WriteEndArray(); // ] context.Response.Write(sw.GetStringBuilder().ToString());

}

public bool IsReusable {
get {
return false;
}
}

}