小编Ves*_*per的帖子

在ASHX AJAX C#中获取JSON

在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>
Run Code Online (Sandbox Code Playgroud)

在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));
}
Run Code Online (Sandbox Code Playgroud)

结果是:

Object {d: null} 
Run Code Online (Sandbox Code Playgroud)

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

c# ajax json ashx

5
推荐指数
1
解决办法
9210
查看次数

标签 统计

ajax ×1

ashx ×1

c# ×1

json ×1