Javascript 非静态字段、方法或属性需要对象引用';System.Web.UI.Control.Context.get';

Javascript 非静态字段、方法或属性需要对象引用';System.Web.UI.Control.Context.get';,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我在编码时遇到了这个错误。下面给出了我的代码,我需要将值传递给jquery以显示数据。此错误是什么“非静态字段、方法或属性”System.Web.UI.Control.Context.get“”需要对象引用” 发生错误的原因是GetData()web服务方法设置为static,只需将其设置为非静态方法即可。另外,您可能需要使用[ScriptMethod(ResponseFormat=ResponseFormat.Json)]属性来修饰该方法,并将返回类型更改为void(当前您没有使用return

我在编码时遇到了这个错误。下面给出了我的代码,我需要将值传递给jquery以显示数据。此错误是什么“
非静态字段、方法或属性”System.Web.UI.Control.Context.get“
”需要对象引用”


发生错误的原因是
GetData()
web服务方法设置为
static
,只需将其设置为非静态方法即可。另外,您可能需要使用
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
属性来修饰该方法,并将返回类型更改为
void
(当前您没有使用
return
语句返回任何内容,但返回类型设置为
string
),如下示例所示:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetData()
{
    List<Glassrecord> glass = new List<Glassrecord>();

    // database connection code here

    JavaScriptSerializer js = new JavaScriptSerializer();

    Context.Response.ContentType = "application/json";
    Context.Response.Write(js.Serialize(glass));
}
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public void GetData()
{
列表玻璃=新列表();
//这里是数据库连接代码
JavaScriptSerializer js=新的JavaScriptSerializer();
Context.Response.ContentType=“应用程序/json”;
Context.Response.Write(js.Serialize(glass));
}

相关问题:

它的never pass in jquery语句,它显示未定义您是否到达方法和
成功
结果(如果
成功
到达,
数据
应包含JSON字符串)?哪个部分返回未定义的
$.ajax({
        url: "Add_Data.aspx/GetData",
        method: "post",
        datatype: "json",
        data: '',
        contentType: "application/json; charset=utf-8",

        success :function(data){
            $('$tblglass').dataTable(
                {
                    data:data,
                    columns: [
                        { 'data': 'Date' }, { 'data': 'GreenTime' }, { 'data': 'YellowTime' }
                    ]
                })
        },

        error: function (err) {
            alert('Object Not Found');
        }
    });
};
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetData()
{
    List<Glassrecord> glass = new List<Glassrecord>();

    // database connection code here

    JavaScriptSerializer js = new JavaScriptSerializer();

    Context.Response.ContentType = "application/json";
    Context.Response.Write(js.Serialize(glass));
}