【问题标题】:Get JSON in ASHX AJAX C#在 ASHX AJAX C# 中获取 JSON
【发布时间】: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"

【问题讨论】:

  • 解决方案是这样的

标签: c# ajax json ashx


【解决方案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 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 2014-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-03
      相关资源
      最近更新 更多