Search code examples
jqueryasp.netashx

jQuery post, posting to ashx page


I am trying to save a user comment to my ashx web handler using jQuery post. The problem is that the form data does not seem to be posted, they got the value of Nothing.

This is a simplified version of my jQuery code:

$(".thread_container input[type='button'][name='simplecomment_submit']").live("click", function(e) {
    var query = "?action=comment_save";
    var url = "script/ajax/myhandler.ashx" + query;
    $.post(url, $('form').serialize(), function(data) {
        if (data == "True") {
            // update comment list
        } else {
            // report error                        
        }
    })
});

My ashx file looks something like this:

Public Class commenthandler : Implements IHttpHandler, IReadOnlySessionState

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim action As String = context.Request.QueryString("action")
        Dim result As String = Boolean.FalseString

        Select Case action      
            Case "comment_save"
                Dim comment As String = context.Request.Form("comment_message")
                ' call save comment here


        End Select

        Return result
    End Sub
End Class

Solution

  • try something like this

    $(".thread_container input[type='button'][name='simplecomment_submit']").live("click", function(e) {
    
        var url = "/script/ajax/myhandler.ashx";
        $.post(url,
              {$('form').serialize(),action:'comment_save'}, 
              function(data) {
                if (data == "True") {
                 // update comment list
                 } else {
                    // report error                        
               }
        }); <-- you missed a semicolon 
    });
    

    and the handler part

    Public Class commenthandler : Implements IHttpHandler, IReadOnlySessionState
    
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Dim action As String = context.Request.QueryString("action")
            Dim result As String = Boolean.FalseString
    
            Select Case action      
                Case "comment_save"
                    Dim comment As String = Context.Request.Form.Get("comment_message")
    
                    ' call save comment here
    
    
            End Select
    
            Return result
        End Sub
    End Class
    

    EDIT

    $(".thread_container input[type='button'][name='simplecomment_submit']").live("click", function(e) {
    
            var url = "/script/ajax/myhandler.ashx";
            var commentText = $("#simplecomment_commenttext").html(); //get the comment text
            $.post(url,
                  {commentTxt:commentText,action:'comment_save'}, 
                  function(data) {
                    if (data == "True") {
                     // update comment list
                     } else {
                        // report error                        
                   }
            }); 
        });
    

    and in the handler do

    Dim action As String = context.Request.QueryString("commentTxt")