Search code examples
c#jsonmootoolsashx

How to read JSONP in ashx


I'm trying to handle a JSONP request server side for a form submission ie.

var myJSONP = new Request.JSONP({
        url: 'http://mysite.../handlers/FormHandler.ashx',
        callbackKey: 'jsoncallback',
        data: {
            partTag: 'mtvo',
            iod: 'hlPrice',
            viewType: 'json',
            results: '100',
            query: 'ipod'
        },
        onRequest: function(url){
            // etc
        },
        onComplete: function(data){
            // etc
        }
    }).send();


 public class FormHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        string json = ??
        JObject j = JObject.Parse(json);


        context.Response.ContentType = "text/json";
        context.Response.Write("Hello World");
    }

I'm not sure how to deserialize in the ashx ie. I use Json.Net but how to get from context Do I have to use context.Request to retrieve values individually or can I decode directly from context?

thanks


Solution

  • I am not sure which JSONP you are using, but using MooTools Request.JSON, the data is delivered in context.Request.Form:

    ?context.Request.Form.ToString()
    "partTag=mtvo&iod=hlPrice&viewType=json&results=100&query=ipod"
    

    So you can access each of the form elements in code:

    ?context.Request.Form["partTag"]
    "mtvo"
    

    Based on this, I believe that you will have to assemble the object yourself using the form elements.