首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在ASHX AJAX C#中获取JSON

在ASHX AJAX C#中获取JSON
EN

Stack Overflow用户
提问于 2014-03-13 17:33:44
回答 3查看 10.5K关注 0票数 5

在Home.aspx中有一个脚本:

代码语言:javascript
复制
<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内部:

代码语言:javascript
复制
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));
}

结果是:

代码语言:javascript
复制
Object {d: null} 

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

EN

回答 3

Stack Overflow用户

发布于 2014-03-14 07:41:52

解决方案是这样的

代码语言:javascript
复制
   <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内部

代码语言:javascript
复制
   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));
    }

与类一起

代码语言:javascript
复制
public class Noticia
    {
        public string Categoria { get; set; }
    }

谢谢你的帮助

票数 0
EN

Stack Overflow用户

发布于 2019-01-04 05:12:32

更改:data: Publicacionesdata: JSON.stringify(Publicaciones),

票数 0
EN

Stack Overflow用户

发布于 2020-04-08 20:25:09

添加

代码语言:javascript
复制
context.Response.ContentType = "application/json";

添加到ASHX方法中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22374316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档