zoukankan      html  css  js  c++  java
  • ajax请求类库

      <system.webServer>
        <handlers>
          <!--type为处理jsonHandler的命名空间加类名-->
          <!--第二个为程序集的名称-->
          <add name="JsonApi" verb="*" path="/Json/*" type="JsonFactory.JsonHandler,JsonFactory" />
        </handlers>

      </system.webServer>


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.SessionState;


    namespace JsonFactory
    {
        public class JsonHandler : IHttpHandler, IRequiresSessionState
        {
            public bool IsReusable
            {
                get { return false; }
            }


            public void ProcessRequest(HttpContext context)
            {
                context.Response.Cache.SetExpires(DateTime.Now);
                context.Response.ContentType = jsonType;
                this.SetResponse(context);
                context.Response.End();
            }


            private const string jsonType = "application/json";
            private const string javascriptType = "application/javascript";




            private void SetResponse(HttpContext context)
            {
                string[] segments = context.Request.Url.Segments;
                string action = "";
                if (segments.Length < 3)
                {
                    return;
                }
                action = segments[2].Trim('/').ToLower();
                JsonBase json = null;
                switch (action)
                {
                    case "user":
                        json = new InformationJsonData(context.Request);
                        break;
                    default:
                        break;
                }
                if (json == null)
                {
                    return;
                }


                string responseString = json.OutputData();


                //var compress = new PageCompression(context);
                //compress.GZipEncodePage();


                context.Response.Write(responseString);
            }
        }
    }


    ajax请求类似路由API(MVC):

     //API路由
                var formatters = GlobalConfiguration.Configuration.Formatters;
                formatters.Remove(formatters.XmlFormatter);
                System.Web.Routing.RouteTable.Routes.MapHttpRoute("DefaultAPi",
                    "api/{controller}/{action}/{id}",
                    new { id = RouteParameter.Optional });

      public class MyController : ApiController
        {
            /// <summary>
            /// 获取单个出团日期日期数据
            /// </summary>
            /// <param name="id"></param>
            /// <returns></returns>
            [AcceptVerbs("POST", "GET")]
            public string GetHelloWorld(int id)
            {
                return "hello world" + id;
            }
          

           $(function () {
                /*
                $.post("/Json/Information/admin", function (d) {
                    alert(d);
                });*/
                var id = 1;
                $.post("/API/My/GetHelloWorld/" + id, function (d) {
                    alert(d);
                });
            })

        }


  • 相关阅读:
    android系统webview使用input实现选择文件并预览
    在列表中动态设置元素的id
    Vue使用Clipboard.JS在h5页面中复制内容
    Vue使用v-for显示列表时,数组里的item数据更新,视图中列表不同步更新的解决方法
    Vue子组件和根组件的关系
    Vue生命周期和钩子函数及使用keeplive缓存页面不重新加载
    Python与数据结构[3] -> 树/Tree[0] -> 二叉树及遍历二叉树的 Python 实现
    Python与数据结构[2] -> 队列/Queue[0] -> 数组队列的 Python 实现
    Python与数据结构[1] -> 栈/Stack[1] -> 中缀表达式与后缀表达式的转换和计算
    Python与数据结构[1] -> 栈/Stack[0] -> 链表栈与数组栈的 Python 实现
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050592.html
Copyright © 2011-2022 走看看