jQuery帖子,发布到ashx页面

时间:2011-08-09 09:42:13

标签: jquery asp.net ashx

我正在尝试使用jQuery post将用户评论保存到我的ashx Web处理程序。问题是表单数据似乎没有发布,它们的值为Nothing

这是我的 jQuery代码的简化版:

$(".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                        
        }
    })
});

我的 ashx文件看起来像这样:

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

1 个答案:

答案 0 :(得分:0)

尝试这样的事情

$(".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 
});

和处理程序部分

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

修改

$(".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                        
               }
        }); 
    });

并在处理程序中执行

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