zoukankan      html  css  js  c++  java
  • .net mvc项目 ajax

    经常在后台用一般处理程序(.ashx)来处理前台的ajax请求

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.Script.Serialization;
    using NewProductImport.Utility;
    using NewProductImport.NewProductImportService;
    using NewProductImport.Common.Models;
    
    namespace MvcApplication4.ajax
    {
        /// <summary>
        /// dele 的摘要说明
        /// </summary>
        public class dele : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                //context.Response.Write("Hello World");
                try
                {
                    /**
                     *     1、如何接受从前台ajax传来的数据
                     *        1.1 context.Request.Form[]得到的数据是字符串,需要用Convert转一下
                     *        1.2 context.Request.Params.Get("dataJsonAdd")
                     *        区别,暂时不知
                     * */
                    int id = Convert.ToInt32(context.Request.Form["id"]);
                    string dataJsonAdd = context.Request.Params.Get("dataJsonAdd");
                    /**
                     *     2、如果接受的数据有json传,需要进行反序列化
                     *        2.1 Newtonsoft.Json.JsonConvert.DeserializeObject<List<ServiceSystemProcess>>(dataJsonAdd);
                     *        2.2 写好泛型,可以直接反序列化成我们需要的类列表
                     * */
                    List<ServiceSystemProcess> = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ServiceSystemProcess>>(dataJsonAdd);
                    /**
                     *     3、调用webservice中的接口,得到返回值
                     *        3.1 DeleteServiceSystemProcess 是Webservice中接口的名字
                     *        3.2 NewProductWS 是在Utility文件夹中的一个webservice类
                     *        3.3 Webservice中的内容这里不细讲
                     * */
                    bool result = NewProductWS.GetInstance().Client.DeleteServiceSystemProcess(header, id);
                      //如果将得到的数据传回到前台ajax,可以对数据进行序列化成json串后再回传。
                    /**
                     *     4、处理完数据,将结果返回给ajax
                     *        4.1 context.Response.Write();
                     *        4.2 如果我们返回的结果不是一个简单的布尔值,而是一个对象,可以先序列化成json传,再返回
                     *            string jsonReturn = Newtonsoft.Json.JsonConvert.serializeObject(回传对象);
                     *            context.Response.Write(jsonReturn);
                     * */
                    context.Response.Write(result);
                }
                catch(Exception ex)
                {
                    //throw ex;
                }
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
  • 相关阅读:
    Java实现 LeetCode 27 移除元素
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 26 删除排序数组中的重复项
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 25 K个一组翻转链表
    Java实现 LeetCode 24 两两交换链表中的节点
    Java实现 LeetCode 24 两两交换链表中的节点
    Java实现 LeetCode 24 两两交换链表中的节点
  • 原文地址:https://www.cnblogs.com/mrxiaohe/p/5220493.html
Copyright © 2011-2022 走看看