首页 > 解决方案 > 如何使用 jQuery AJAX 调用 ASP.Net 字符串函数?

问题描述

我有一个字符串函数 ASP.Net Webform。我想使用 AJAX 调用这个函数。该函数从数据库中返回一个带有月份索引的字符串值

protected string BringDatas(int month)
{

  Counts counts_ = new Counts();
  return counts_.GetMonths(month);
}
var dataValue = { "month": 1 };
$.ajax({
  type: "POST",
  url: "Homepage.aspx/BringDatas",
  data: dataValue,
  error: function (XMLHttpRequest, textStatus, errorThrown) {
    alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
  },
  complete: function (jqXHR, status) {
    alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
  }
});

标签: c#jqueryajaxasp.net-mvcwebforms

解决方案


这是javascript方面

<script type="text/javascript">
    $(document).ready(
        function () {
            $("#Gonder").click(
                function () {
                   
                    $.ajax
                        ({
                            type: "POST",
                            url: "Homepage.aspx/OrnekPost",
                            data: "{'parametre':'1234'}",
                            contentType: "application/json; charset=utf-8",
                            dataType: "text",
                            success: function (output) {
                                alert("Response: "+ output);
                            }, error: function () {
                                alert("hata var");
                            }
                        });
                });
        })
</script>

Codebehind.cs 代码

[ScriptMethod]
        [WebMethod(EnableSession = true)]
        public static string OrnekPost(string parametre)
        {
            return parametre + " değeriyle post işlemi gerçekleştirildi.";
        }

推荐阅读