【问题标题】:How to call non static method from AJAX?如何从 AJAX 调用非静态方法?
【发布时间】:2012-09-14 06:29:08
【问题描述】:

这是我从 AJAX 调用的代码...

    [WebMethod]
    [ScriptMethod]
    public static string save(string parameter)
    {
        country_master obj_country = new country_master();
        obj_country.Country_Name = Page.Request.Params["name"].ToString().Trim();
        obj_country.saved();
        return "";
    }

这里我无法访问通过 Page.Request 从页面传递的参数。

string name = HttpContext.Current.Request.QueryString["name"].Trim();
return "error";

在写完第一行之后,return 语句不会向 AJAX 返回任何内容。 请帮助我如何做到这一点。 谢谢...

【问题讨论】:

  • 不,您不能从 ajax、asp.net webforms 中调用非静态方法并访问 Context 属性,您可以使用 HttpContext 静态类。
  • obj_country.Country_Name = HttpContext.Current.Request.QueryString['name'].Trim()
  • 感谢帮助 country_master obj_country = new country_master(); obj_country.Country_Name = HttpContext.Current.Request.QueryString["name"].Trim();返回“错误”;写完这些代码返回语句后不会向 AJAX 返回任何东西。请帮忙...

标签: asp.net ajax json


【解决方案1】:

要获取当前上下文,您可以使用HttpContext.Current,这是一个静态属性。

一旦你有了它,你就可以访问诸如会话或个人资料之类的东西,并获取有关网站状态的信息

HttpContext.Current.Session等..

此链接可能对您有所帮助:Call Server Side via AJAX without a Static Method

将 web 方法限制为静态的原因是为了避免它访问实例页面的控件。

【讨论】:

  • 感谢帮助 country_master obj_country = new country_master(); obj_country.Country_Name = HttpContext.Current.Request.QueryString["name"].Trim();返回“错误”;编写完这些代码返回语句后不会向 AJAX 返回任何内容。请帮忙...
【解决方案2】:

你可以使用HttpContext.Current静态类,但是如果你在你的方法中声明你想要使用的参数,你可以跳过它,只需通过你的AJAX调用传递参数

您应该将参数直接传递给方法。

我有几个工作示例on my Github repository,请随意浏览代码。

总结一下,调用一个PageMethod:

注意:jobID PageMethod 参数如何使用 AJAX 与请求一起传递,以及如何在 PageMethod 内部透明地使用

AJAX 调用

             $.ajax({
                type: 'POST',
                url: '<%: this.ResolveClientUrl("~/Topics/JQuery/Ajax/PageMethods_JQueryAJAX.aspx/GetEmployees") %>',
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                data: '{"jobID" : ' + jobID +'}',
                async: false,
                cache: false,
                success: function (data) {
                    $('#employees').find('option').remove();
                    $.each(data.d, function (i, item) {
                        $('<option />').val(item.EmployeeID).text(item.FirstName).appendTo('#employees');
                    });
                },
                error: function (xhr) {
                    alert(xhr.responseText);
                }
            });

页面方法

    [WebMethod]
    public static List<EmployeeModel> GetEmployees(int jobID)
    {
        var ctx = new PubsDataContext();

        return (from e in ctx.employee
                where e.job_id == jobID
                orderby e.fname
                select new EmployeeModel
                {
                    EmployeeID = e.emp_id,
                    FirstName = e.fname
                }).ToList();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    相关资源
    最近更新 更多