.net Ajax与后台一般处理程序(ashx) 交互

本文主要实现无动态刷新查询后台数据功能,主要用到ajax+ashx+sqlserver进行交互.

首先需要引用Jquery:

 
复制代码

后台来接收前台传过来的值,对其进行操作:

复制代码
     public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string corpName = context.Request["corpName"].Trim().ToString();      //接收前台传过来的参数
            if (corpName != "")
            {
                string sql = @"select top 1 CorpName,CreditCode,CorpCode,LinkPhone,StateName,AdminAreaName,LinkMan,LinkMobile,Address from tbProductOrder as a 
                                        inner join tbStateDic as b on a.StateNum = b.StateNum
                                        inner join tbAdminAreaClass on a.CityNum = AdminAreaClassID
                                        where CorpName =@CorpName order by CorpName";
                SqlParameter[] par = new SqlParameter[1];
                par[0] = new SqlParameter("@CorpName", corpName);
                DataSet ds = DBHerpler.Load(sql, par); 
                string json = SerializerHelper.ToJsonString(ds.Tables[0]);    //返回json类型的数据
                context.Response.Write(json);                      
                context.Response.End(); 
            } 
        }
复制代码

SerializerHelper类的定义:

 

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using Newtonsoft.Json;

/// 
/// SerializerHelper 的摘要说明
/// 
public static class SerializerHelper
{
    /// 
    /// 反序列化XML文件
    /// 
    public static T LoadFromXmlFile(string filepath) where T : class
    {
        using (FileStream stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T)serializer.Deserialize(stream);
        }
    }

    /// 
    /// 反序列化XML字符串
    /// 
    public static T LoadFromXmlString(string xml) where T : class
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        byte[] bytes = Encoding.UTF8.GetBytes(xml);

        using (MemoryStream stream = new MemoryStream(bytes))
        {
            return (T)serializer.Deserialize(stream);
        }
    }

    /// 
    /// 序列化XML对象
    /// 
    public static string SaveToXmlString(T entity) where T : class
    {
        using (MemoryStream stream = new MemoryStream())
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(stream, entity);
            return Encoding.UTF8.GetString(stream.ToArray());
        }
    }

    /// 
    /// 序列化Json对象
    /// 
    public static string ToJsonString(object obj)
    {
        return ToJsonString(obj);
    }

    /// 
    /// 序列化Json对象
    /// 
    public static string ToJsonString(T obj) where T : class
    {
        string text = JsonConvert.SerializeObject(obj);
        return text;
    }

    /// 
    /// 反序列化Json字符串
    /// 
    public static T ToJsonObject(string text) where T : class
    {
        T obj = (T)JsonConvert.DeserializeObject(text, typeof(T));
        return obj;
    }
} 
  
复制代码

 

如果向后台传入多个参数在data里面用逗号分割可写多个参数:

    data: { corpName: corpName , corpName2: corpName2} 

你可能感兴趣的:(.net Ajax与后台一般处理程序(ashx) 交互)