Get JSON in ASHX AJAX C#

post data to ashx handler
get json data in handler
how to pass parameter to generic handler in c#
generic handler in asp.net c# example
call ashx handler from jquery
ashx return json
ashx get post data

Have a script in 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>

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

And the result is :

Object {d: null} 

Why this result? if i send a parameter in the data with the variable Publicaciones with value "Noticia".

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

ASP.NET - Passing JSON from jQuery to ASHX, You can read the JSON object on the server with this Server Side .ashx format, like C# String.prototype.format = String.prototype.f = function() { var str = this; for (var i = 0; if using $.ajax and using .ashx to get querystring ,dont set datatype Now we add a userInfo class into our generic handler ( ashx file ), which matches with our client side JSON object. // we create a userinfo class to hold the JSON value public class userInfo { public string first_name { get; set; } public string last_name { get; set; } public string qualification { get; set; } public string age { get; set; } }

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

Send and Parse JSON Data from ASHX File, I like to share a small knowledge like how to send JSON data by JQuery ajax to ashx file and how to parse that JSON data from ashx file and  Overview: Here in this article will see asp.net jQuery Ajax JSON example, i.e. calling jQuery Ajax WebMethod and get JSON format result (JSON data) in Asp.net C# Webform. In short a simple jQuery Ajax JSON example in Asp.net C# with MS SQL Server database.

Add

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

To your ASHX method.

How to read JSON data from ashx file, so please help me how to read data from ashx file when json send from client $​.ajax({ type: "POST", url: urlToHandler + "?ac=send", data:  Here Mudassar Ahmed Khan has explained with an example, how to Call (Consume) JSON Generic HTTP Handler (ASHX) on Client Side using jQuery AJAX in ASP.Net. When the JSON Generic HTTP Handler will be called using jQuery AJAX, it will pull records from database and return the records in JSON format to jQuery AJAX function. TAGs: ASP.Net, AJAX, jQuery, JSON, Generic Handler

Generic Handler ashx file : Post send JSON data in Asp.net c#, jQuery, You may notice in below client-side code under ajax method we have used `data: jsonData,` this is important here bcoz we are going to send /  I am passing json data to my generic handler page GenericHandler.ashx.cs using jquery ajax request and json as data type. in my handler code i would like to return html table in string format. here is snap of my handler code

Call (Consume) JSON Generic HTTP Handler (ASHX) using jQuery , AJAX function. TAGs: ASP.Net, AJAX, jQuery, JSON, Generic Handler. WebHandler Language="C#" Class="Handler" %>. using System;. This structure works for me - I used it in a small tasks management application. The controller: public JsonResult taskCount(string fDate) { // do some stuff based on the date // totalTasks is a count of the things I need to do today // tasksDone is a count of the tasks I actually did // pcDone is the percentage of tasks done return Json(new { totalTasks = totalTasks, tasksDone = tasksDone

ASP.NET - Passing JSON from jQuery to ASHX - asp.net - html, ASP.NET - Passing JSON from jQuery to ASHX - asp.net. return str; }; })(jQuery​); c# public class ReportHandler : IHttpHandler { private const string if using $.​ajax and using .ashx to get querystring ,dont set datatype $.ajax({ type: "POST",  Make sure that you always check for null or empty strings in the context.Request Object and your response should be a JSON Object. Construct into JSON before sending from the.ashx handler like Your ajax should look like this,

Comments
  • The solution was this
  • I get an exception when using JsonConvert.DeserializeObject which returns, "Cannot deserialize the current JSON array (e.g.[1,2,3]) into type 'SurplusApp.items' because the type requires a JSON object.
  • What would this look if the object contained an array of Publicaciones?