Get JSON in ASHX AJAX C# Get JSON in ASHX AJAX C# json json

Get JSON in ASHX AJAX C#


The solution was this

   <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>

inside of 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));    }

with the class

public class Noticia    {        public string Categoria { get; set; }    }

Thanks for help me


Change: data: JSON.stringify(Publicaciones),For: data: Publicaciones


Add

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

To your ASHX method.