tags:

views:

116

answers:

2

I am making a jquery call from a javascript method. I want a parameter to be sent to my call back method. I am using a handler(ashx) to make jquery call, the handler is getting invoked but the callback is not getting fired. Below is the code

function MyButtonClick(){
var myDiv = "divname";
$.post("MyHandler.ashx", { tgt: 1 }, myDiv, CustomCallBack);
}

function CustomCallBack(data, result) {
        debugger;
        //SomeCode
    }
}

Handler code(ashx file)

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            int tgt = Convert.ToInt32(context.Request["tgt"]);
            if (tgt == 1)
            {
                          context.Response.Write("Some text");
            }
        }
A: 

Have you had a look at this page : http://docs.jquery.com/How_jQuery_Works#Callback_with_arguments

Should you be using a $.get(... call?

    $.get("MyHandler.ashx", { tgt: 1 }, myDiv, function() {
        CustomCallBack(data, result);
    });
Daniel Dyson
Khan is using C# not PHP
Jason Jong
Hi @Jason Not PHP. JQuery. The question is not very clear about which handler is not being called. The MyHandler.ashx or the Callback Handler. Good guess that it is the ashx handler. I will amend the question to make it more clear.
Daniel Dyson
its not working
KhanS
A: 

In your ashx, you need to get the data from reading the post data...

    StringBuilder HttpInputStream;

    public void ProcessRequest(HttpContext context)
    {

        HttpInputStream = new StringBuilder();
        GetInputStream(context);

       // the data will be in the HttpInputStream now
       // What you might want to do it to use convert it to a .Net class ie,
       // This is using Newtonsoft JSON
       // JsonConvert.DeserializeObject<JsonMessageGet>(HttpInputStream.ToString());

    }

    private void GetInputStream(HttpContext context)
    {
        using (Stream st = context.Request.InputStream)
        {
            byte[] buf = new byte[context.Request.InputStream.Length];
            int iRead = st.Read(buf, 0, buf.Length);
            HttpInputStream.Append(Encoding.UTF8.GetString(buf));
        }
    }

In your response back the caller - your ashx needs to respond in JSON so the caller JavaScript can consume it.

I suggest using Newstonsoft JSON eg...

 public void ProcessRequest(HttpContext context)
 {
     // read from stream and process (above code)

     // output
     context.Response.ContentType = "application/json";
     context.Response.ContentEncoding = Encoding.UTF8;
     context.Response.Write(JsonConvert.SerializeObject(objToSerialize, new IsoDateTimeConverter()));
 }

OR to make it really really easy, change to - basically needs to respond back in JSON for the JavaScript handler to process it

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.Write("{data:\"Reply message\"}");
    }
Jason Jong