jQuery 如何通过 ASP.Net context.Request 检索 JSON

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3398926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:22:52  来源:igfitidea点击:

How to retrieve JSON via ASP.Net context.Request

jqueryasp.netcontextmenu

提问by agentpx

var OrderInfo = {"ProductID": 
    "ProductIDValue",
    "ProductName": "ProductName",
    "Quantity": 1,
    "Amount": 9999,
    "SLQuantity": 9999,
    "SLDate": "08/03/2010"
};

var DTO = { 'OrderInfo': OrderInfo };
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "JasonHandler.ashx",
    data: JSON.stringify(DTO),
    dataType: "json"
 });

I'm trying to retrieve posted JSON data on server side in an ASHX file via this code:

我正在尝试通过以下代码在 ASHX 文件中检索服务器端发布的 JSON 数据:

string strrequest = context.Request["OrderInfo"];

but it always return null. What Am I doing wrong?

但它总是返回空值。我究竟做错了什么?

采纳答案by ronaldwidha

  1. get the request body from HttpContext.Current.Request.InputStream.
  2. read the input stream and convert to string
  3. use javascriptserializerto deserialize the json object to a strongly type object (ensure the json properties share the same name as the strongly type counter part)
  1. HttpContext.Current.Request.InputStream获取请求正文。
  2. 读取输入流并转换为字符串
  3. 使用javascriptserializer将 json 对象反序列化为强类型对象(确保 json 属性与强类型计数器部分共享相同的名称)

回答by agentpx

Digging the Internet. I found out that IE has problem receiving POST request in full. @ronaldwidha's suggestion on InputStream is similar to what I have found. But rather than using javascriptserializer I use JSON.NET Code snippets is below and I hope this would help other with similar problem

挖掘互联网。我发现 IE 无法完全接收 POST 请求。@ronaldwidha 对 InputStream 的建议与我发现的相似。但是我没有使用 javascriptserializer,而是使用下面的 JSON.NET 代码片段,我希望这能帮助其他有类似问题的人

 public class JasonHandler : IHttpHandler {

 public void ProcessRequest (HttpContext context) {

    context.Response.ContentType = "application/json";
    context.Response.ContentEncoding = Encoding.UTF8;

    System.IO.Stream body = context.Request.InputStream;
    System.Text.Encoding encoding = context.Request.ContentEncoding;
    System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
    if (context.Request.ContentType != null)
    {
        context.Response.Write("Client data content type " + context.Request.ContentType);
    }
    string s = reader.ReadToEnd();
    string[] content = s.Split('&');
    for (int i = 0; i < content.Length; i++)
    {
        string[] fields = content[i].Split('=');
        //context.Response.Write("<div><strong>" + fields[0] + "</strong></div>");
        //context.Response.Write("<div>" + fields[1] + "</div> ");  
    }

    string jsonRecord = s;
   }
}

回答by Peter

From http://dailydotnettips.com/2013/09/26/sending-raw-json-request-to-asp-net-from-jquery/

来自http://dailydotnettips.com/2013/09/26/sending-raw-json-request-to-asp-net-from-jquery/

var jsonString = String.Empty;

context.Request.InputStream.Position = 0;
using (var inputStream = new StreamReader(context.Request.InputStream))
{
jsonString = inputStream.ReadToEnd();
}

JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
object serJsonDetails = javaScriptSerializer.Deserialize(jsonString, typeof(object));

回答by Mike

Request[] will only look at form params and quetystring. You will need to do a form post or use qs or parse the request body yourself.

Request[] 只会查看表单参数和查询字符串。您将需要进行表单发布或使用 qs 或自己解析请求正文。

回答by Frank Schwieterman

I think you could get the request body out of HttpCurrent.Context.Request.GetResponse().

我认为您可以从 HttpCurrent.Context.Request.GetResponse() 中获取请求正文。

Its probably a good idea to verify the content-type header first.

首先验证内容类型标头可能是个好主意。