C# 使用 jquery ajax 在 aspx.cs 文件中调用 webmethod

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10688951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:50:07  来源:igfitidea点击:

Calling webmethod ina aspx.cs file using jquery ajax

c#jqueryasp.netajaxwebmethod

提问by Luke Villanueva

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.

我有一个 default.aspx.cs,其中包含要调用的 webmethod,并且我的 js 文件包含我的 jquery ajax。我无法调用 webmethod。

Here is my default.aspx.cs:

这是我的 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:

这是我的 js 文件:

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! :)

您可能会注意到我正在提醒我的数据以确保我有正确的值发送到我的 webmethod。但就像我说的那样,它无法调用我的 webmethod,也不会在我的 ajax 中执行我的成功功能。帮助!谢谢!:)

采纳答案by Claudio Redi

You webmethod needs to be static.

你的 webmethod 需要是static.

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

回答by Asif Mushtaq

Try to set typeto "Get" and send the parameters in the URL instead of Data

尝试设置type为“获取”并在 URL 中发送参数而不是数据

url: "Default.aspx/getDetails/?colunmname="+colname+"&inputVal="+inputValue,
type: "GET"

回答by Zhan

This one is a complete sample, which shows the whole process at the beginning until at the end of how to call a server side "webmethod" via ajax request using asp.net page.

这是一个完整的示例,展示了如何使用asp.net页面通过ajax请求调用服务器端“webmethod”的整个过程。

http://www.codeproject.com/Questions/374136/Call-Page-Method-From-Jquery-Ajax-Call

http://www.codeproject.com/Questions/374136/Call-Page-Method-From-Jquery-Ajax-Call