4

i have an ASP.NET 4.0 HTTP handler that should receive and send data in json format. I'm using jquery to send json objects serialized in a string to the handler. It correctly sends the request but i don't know how i could retrieve the data from the httpcontext passed to the handler and how i could deserialize it... Can someone help me?

UPDATE 1

$.ajax({
    type: "POST",
    url: "myurl.ashx",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    data: $.toJSON({
        ...
    }),
    success: function (response) {
        ...
    }
});
1
  • I don't know the toJSON plugin, but what i know is that data expects a key/value pair object where the key is what you'll request from the Forms collection. So i guess the question is know, what your $.ToSJON call returns. Commented Nov 14, 2010 at 22:16

1 Answer 1

4

Do you send the data from jquery as a POST or GET request? In your Http Handler you can retrieve the values through the HttpContext.Request either via Forms or QueryString

ie. string json = HttpContext.Current.Request.Forms["json"];

To deserialize you can use the built in System.Web.Script.Serialization.JavaScriptSerializer class like this

string json = HttpContext.Current.Request.Forms["json"];
var js = new JavaScriptSerializer();
YourType obj = js.Deserialize<YourType>(json);
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sending the data as a POST request... if it can help, i'm trying to make a really simple comet c# server implemented over the bayeux protocol. I don't know if in this case it's better to send the data as a GET request but anyway in the Request.Form collection there are 0 items
If you're sending as POST the Forms-collection should not be null. How does your jquery-code look like?
this is a very old post, but I thought I'd point to a good answer --the forms collection should be empty, and you need to parse the request input stream; the answer is most likely here stackoverflow.com/questions/6421351/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.