在 ASHX AJAX C# 中获取 JSON

Posted

技术标签:

【中文标题】在 ASHX AJAX C# 中获取 JSON【英文标题】:Get JSON in ASHX AJAX C# 【发布时间】:2014-04-17 22:23:39 【问题描述】:

在 Home.aspx 中有一个脚本:

<script type="text/javascript">
  function probarAjax() 

    var Publicaciones = 
      "Categoria": "Noticia"
    

    $.ajax(
      type: "POST",
      url: "Controlador.ashx?accion=enviar",
      data: JSON.stringify(Publicaciones),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function(data) 
        console.log(data);
      ,
      error: function(XMLHttpRequest, textStatus, errorThrown) 
        alert(textStatus);
      

    );
  
</script>

Controlador.ashx 内部:

public void ProcessRequest(HttpContext context) 
  context.Response.ContentType = "text/json";

  var categoria = string.Empty;
  JavaScriptSerializer javaSerialize = new JavaScriptSerializer();
  categoria = context.Request["Categoria"];

  var capaSeguridad = new  d = categoria ;

  context.Response.Write(javaSerialize.Serialize(capaSeguridad));

结果是:

Object d: null 

为什么会有这个结果?如果我在数据中发送一个参数,变量Publicaciones,值为"Noticia"

【问题讨论】:

解决方案是这样的 【参考方案1】:

解决办法是这样的

   <script type="text/javascript">
    function probarAjax() 

        var Publicaciones = 
               "Categoria" : "Noticia"                  
        

        $.ajax(
            type: "POST",
            url: "Controlador.ashx?accion=enviar",
            data: JSON.stringify(Publicaciones),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) 
                console.log(data.d);
            ,
            error: function (XMLHttpRequest, textStatus, errorThrown) 
                alert(textStatus);
            


        );
        

</script>

ashx内部

   public void ProcessRequest(HttpContext context)
    
        context.Response.ContentType = "text/json";

        System.IO.Stream body = context.Request.InputStream;
        System.Text.Encoding encoding = context.Request.ContentEncoding;
        System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);

        string s = reader.ReadToEnd();
        Noticia publicacion = JsonConvert.DeserializeObject<Noticia>(s);
        var capaSeguridad = new  d = publicacion.Categoria ;

        context.Response.Write(JsonConvert.SerializeObject(capaSeguridad));
    

跟班

public class Noticia
    
        public string Categoria  get; set; 
    

谢谢你的帮助

【讨论】:

我在使用 JsonConvert.DeserializeObject 时遇到异常,它返回“无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'SurplusApp.items',因为该类型需要 JSON对象。 如果对象包含 Publicaciones 数组,这会是什么样子?【参考方案2】:

更改:data: JSON.stringify(Publicaciones), 对于:data: Publicaciones

【讨论】:

【参考方案3】:

添加

context.Response.ContentType = "application/json";

到您的 ASHX 方法。

【讨论】:

以上是关于在 ASHX AJAX C# 中获取 JSON的主要内容,如果未能解决你的问题,请参考以下文章

C#实现简易ajax调用后台方法

JQuery POST json 数据在后台怎么获取数据?

如何使用 c# 从 ajax 中的嵌套 JSON 对象中获取值

AJAX提交到Handler.ashx一般处理程序返回json数据-转

51.Qt-使用ajax获取ashx接口的post数据

ASP.NET前台table通过Ajax获取绑定后台查询的json数据