javascript - 如何使用 jQuery 调用特殊函数?

标签 javascript jquery function post

我试图通过调用方法在 jQuery 中通过 post 方法获取字符串。 这是我的代码:

 $("#btn_Submit").submit(function () {
     $.ajax({
         type: "POST",
         url: "frm_GetTable.aspx/GetComment",
         data: "{}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (data) {
             // Replace the div's content with the page method's return.
             $("#Comment").empty().html(data);                     
         }
     });

这是我的职责:

protected String GetComment ()
    {
        String Comment="";


        Comment=(" <table>");
        foreach (DataRow dr in dt_Comment)
        {
            Comment +=("<tr ><td>  </td><td rowspan='2'> " + dr["Com"].ToString() + " </td></tr>");
            Comment += ("<tr><td >" + dr["Date"].ToString() + "</td></tr>");

        }
        Comment += ("</table>");
        return Comment;
    }

当我提交时显示错误:状态信息对于此页面无效并且可能已损坏。 请帮忙,我不明白问题出在哪里。

最佳答案

  1. 你应该创建函数public static
  2. 为函数添加[WebMethod]属性

    [WebMethod]
    public static string GetComment()
    {
        String Comment = "";
        Comment = (" <table>");
        foreach (DataRow dr in dt_Comment)
        {
            Comment += ("<tr ><td>  </td><td rowspan='2'> " + dr["Com"].ToString() + " </td></tr>");
            Comment += ("<tr><td >" + dr["Date"].ToString() + "</td></tr>");
        }
        Comment += ("</table>");
        return Comment;
    }
    

关于javascript - 如何使用 jQuery 调用特殊函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16979093/

相关文章:

javascript - 如何在Javascript中获取document.onkeydown函数的名称?

javascript - CORS ajax请求说明

Jquery prependTo 添加显示 :block

javascript - React 组件在屏幕上打印两次

javascript - 将全局函数包装在 (function($){ }(jQuery) 中

jquery - 通过 jQuery 将 url 发送到 iframe

php - 将 php 函数的返回值传递给 mysql_query

c - 正在读取文件,但未保存

javascript - null 为 null 或不是对象 javascript 错误

javascript - AJAX 请求的安全性