Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c# - Calling webmethod ina aspx.cs file using jquery ajax

I have a default.aspx.cs which contains my webmethod to call and I have my js file that containg my jquery ajax. I can't get to call the webmethod.

Here is my default.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        string[] MyArray = new string[1];
        MyArray[0] = "My Value";

        Grid1D.DataSource = MyArray;
        Grid1D.DataBind();
    }

    [WebMethod]
    public Details[] getDetails(string columnname, string inputVal)
    {
        List<Details> list = new List<Details>();

        DbAccess dbacc = new DbAccess();

        DataTable dt = dbacc.getReportDetails(columnname, inputVal);

        foreach (DataRow row in dt.Rows)
        {
            Details _Details = new Details();
            _Details.memid = row["memid"].ToString();
            _Details.usrname = row["usrname"].ToString();
            _Details.fullname = row["fullname"].ToString();
            _Details.fname = row["fname"].ToString();
            _Details.mname = row["mname"].ToString();
            _Details.lname = row["lname"].ToString();
            _Details.bdate = row["bdate"].ToString();
            _Details.address = row["address"].ToString();
            _Details.sponsorid = row["sponsor_id"].ToString();
            _Details.parentid = row["parent_id"].ToString();
            _Details.placement = row["placement"].ToString();
            _Details.datejoined = row["date_joined"].ToString();


            list.Add(_Details);
        }

        Grid1D.DataSource = list.ToArray();
        Grid1D.DataBind();

        return list.ToArray();
    }

And here is my js file:

function retrieveReportData() {
    var columnName = $("#ddlFilters").val();
    var input = $("#tags").val();

    if (columnName != "Select") {

        var Data = JSON.stringify({ columnname: columnName, inputVal: input });

        alert(Data);

        $.ajax({

            url: "Default.aspx/getDetails",
            data: Data,
            type: "POST",
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success: function (mydata) {

                alert(mydata.d);
            }
        });

    }
    else 
        alert("Please choose search filter");
}

You may notice that I'm alerting my data to ensure that I have the right values to send to my webmethod. But just like I said, it fails to call my webmethod and don't proceed to my success function within my ajax. Help! Thanks! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You webmethod needs to be static.

[WebMethod]
public static Details[] getDetails(string columnname, string inputVal)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...