zoukankan      html  css  js  c++  java
  • .ashx获取post body

     
    前台页面代码:
    var json = [{ "Name": "Pavan Kumar Pabothu", "Age": 27, "ID": 361621 }, { "Name": "Reddaiah Raju Padhmaraju", "Age": 27, "ID": 362541 }, { "Name": "Denish Raju Padhmaraju", "Age": 26, "ID": 368941 }];
    $.ajax({
    type: "POST",
    // url: "CommonFiles.aspx/DeleteFiles",
    url: "../data/AjaxTest.ashx",
    //data: "{Id:'" + deleteid + "'}",
    data: { 'myjson': json },
    contentType: "application/json; charset=utf-8",
    dataType: 'json',
    success: function (data) {
    var result = data.d;
    if (result == "error")//失败
    {
    }
    loadFileSort(id);
    }
    });
    Ashx 代码:
     
    StringBuilder rsb = new StringBuilder();
    var mRequest = context.Request;
    int bytelengg=(int)mRequest.InputStream.Length;
    using (var reader = new StreamReader(mRequest.InputStream,Encoding.UTF8))
    {
    var read = new Char[bytelengg];
    var count = reader.Read(read, 0, bytelengg);
    while (count > 0)
    {
    var str = new string(read, 0, count);
    rsb.Append(str);
    count = reader.Read(read, 0, bytelengg);
    }
    reader.Close();
    reader.Dispose();
    mRequest.InputStream.Close();
    mRequest.InputStream.Dispose();
    }
    然后通过反序列字符串转换成对
     
     
    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType = "text/plain";
    //context.Response.Write(context.Request.Form["param2"]);
    HttpRequest request = context.Request;
    Stream stream = request.InputStream;
    string json = string.Empty;
    string responseJson = string.Empty;
    if (stream.Length != 0)
    {
    StreamReader streamReader = new StreamReader(stream);
    json = streamReader.ReadToEnd();
    context.Response.Write(json);
    }
    context.Response.Write("没有获取到json");
    }
     
    参数方式:
    context.Response.ContentType = "text/plain";
                if (context.Request.QueryString["My"] != null)
                {
                    context.Response.Write("参数:"+ context.Request.QueryString["My"]);
                }
                else
                {
                    context.Response.Write("没有参数!Hello World");
                }
  • 相关阅读:
    java 继承(下)
    java继承
    java代码封装与编译
    使用Access-Control-Allow-Origin解决跨域
    java (基本语法)
    ZendStudio如何汉化
    如何让数据库在每天的某一个时刻自动执行某一个存储过程或者某一个sql语句
    百度地图不用密匙也可以使用
    .net在当前日期的基础上加一天
    当你的IIS需要运行ASP网站时,需要这样配置下你的IIS
  • 原文地址:https://www.cnblogs.com/devgis/p/14180841.html
Copyright © 2011-2022 走看看