如何从AJAX调用非静态方法?

Chi*_*hel 4 asp.net ajax json

这是我从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 "";
    }
Run Code Online (Sandbox Code Playgroud)

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

string name = HttpContext.Current.Request.QueryString["name"].Trim();
return "error";
Run Code Online (Sandbox Code Playgroud)

在写完第一行之后,return语句不会向AJAX返回任何内容.请帮我解决这个问题.谢谢...

Vis*_*har 5

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

完成后,您可以访问会话或个人资料等内容,并获取有关网站状态的信息

HttpContext.Current.Session 等等..

此链接可以帮助您:在没有静态方法的情况下通过AJAX调用服务器端

限制Web方法为静态的原因是避免它访问实例页面的控件.