• JQuery+post+asp.net的三种方法


    
        无标题页
        
        
        
        
        
        
        
    

    panel content.

    panel content.

    前两种方法带不带

    都无所谓,第三种一定要带,另外,我们在JQuery中定义POST了,所以form里写不写method=post也无所谓了

    下面文件,我写在ashx中

    <%@ WebHandler Language="C#" Class="Handler_login" %>
    
    using System;
    using System.Web;
    using System.Text;
    public class Handler_login : IHttpHandler
    {
        public string name ="";
        public string pwd = "";
        
        public void ProcessRequest (HttpContext context) {
            context.Response.ContentType = "text/plain";//无格式正文
            if ((context.Request["name"] != null) && (context.Request["pwd"] != null))
            {
                this.name = context.Request["name"].ToString();
                this.pwd = context.Request["pwd"].ToString();
                
                context.Response.ContentType = "application/json;charset=utf-8";
                context.Response.Write(ToJson(new string[]{"name@"+name,"pwd@"+pwd}));
                //可以增加一个带bool值:context.Response.Write(ToJson(new string[] { "success@" + true, "name@" + name, "pwd@" + pwd }));         }
        }
     
        public bool IsReusable {
            get {
                return false;
            }
        }
    
        public string ToJson(string[] array)
        {
            StringBuilder s = new StringBuilder(128);
            s.Append("[");
            for (int i = 0; i < array.Length; i++)
            {
                s.Append("{");
                s.Append("\"");
                s.Append(array[i].Split('@')[0]);
                s.Append("\"");
                s.Append(":");
                s.Append("\"");
                s.Append(array[i].Split('@')[1]);
                s.Append("\"");
                s.Append("}");
                if (i != (array.Length-1))
                s.Append(",");
            }
            s.Append("]");
            return s.ToString();
        }
    }

    ToJson 是为了生成Json用的,注意下,Json的格式为[{"name":"123"}.{"pwd":"123"}],这里的必须用的双引号,不然JQuery就显示一个错误,收不到服务器端传回的数据.

  • 相关阅读:
    共模电感有什么作用与选型技巧?|深圳比创达EMC
    oracle开启归档日志并修改归档日志路径
    威视佰科荣获:2023年“AI天马”高成长性企业
    华为云云耀云服务器L实例评测|部署个人音乐流媒体服务器 navidrome
    OpenSSF的开源软件风险评估工具:Scorecards
    httpserver 下载服务器demo 以及libevent版本的 httpserver
    TCP相关面试题
    TiDB 环境与系统配置检查
    【C++】 string类常用接口的实现
    java 通过Iterator实现Collection集合遍历
  • 原文地址:https://blog.csdn.net/xxpr_ybgg/article/details/127748694