如何从vb.net中的aspx.vb页面调用webmethod

Muh*_*aid 6 javascript vb.net asp.net jquery

我想做的是从aspx.vb调用WebMethod,下面是Default.aspx.vb中的WebMethod语法

<System.Web.Services.WebMethod()> _
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function dat( _
ByVal Id As Integer) As List(Of items)
    Dim eve As New List(Of items)()
    eve = (From row In getItems(Id).Rows
           Select New items With {
                                .Name = row("Name").ToString(),
                                .Description = row("Description").ToString(),
                                .ItemPic_url = row("ItemPic_url").ToString()}).ToList()
    Return eve
End Function
Run Code Online (Sandbox Code Playgroud)

以下是我从中调用Web方法的jquery函数:

注意:我的Jquery函数位于我的母版页中,我从启动Default.aspx页调用它。

function getItems() {
        $("#tbody").empty();
        var id = $("select")[0].value;
        $.ajax({
            url: "Default.aspx/dat",
            data: { Id: id },
            contentType: "Application/json; charset=utf-8",
            responseType: "json",
            method: "POST",
            success: function (response) {
                $("#tbody").empty();
                var rows = response.d;
                var count = response.d.length;
                var table = document.getElementById("tbody");
                var row;
                var cell;
                for (var i = 0; i < rows.length; i++) {
                    if (i % 4 == 0) {
                        row = table.insertRow();
                    }
                    cell = row.insertCell();  //simply insert the row
                    cell.innerHTML = "<td><ul><li style='text-align:center;'><img id='imgload' width='190px'; height='166px' src='../Images/CatalogImgs/" + rows[i].ItemPic_url + "' alt='No Image Found' /></li><li style='margin:4px 6px;font-weight: 600;font-family: Calibri;font-size: 16px;'>" + rows[i].Name + "</li><li style='margin:4px 6px;color: #808080;font-weight: 600;'><p>" + rows[i].Description + "</p></li></ul></td>";
                    if (document.getElementById("tbody").rows[0].cells.length > 0)
                    {
                        //alert(document.getElementById("tbody").rows[0].cells.length);
                        switch (rows.length) {
                            case 1:
                                $("#tbody > tr > td").css('padding-left', '18%');
                                break;
                            case 2:
                                $("#tbody > tr > td").css('padding-left', '12%');
                                break;
                            case 3:
                                $("#tbody > tr > td").css('padding-left', '6%');
                                break;
                            default:
                                $("#tbody > tr > td").css('padding-left', '1%');
                        }
                    }
                }
            },
            error: function (xhr) {
                alert(xhr.status);
            },
            Failure: function (response) {
                alert(response);
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

问题:我没有输入Web方法。通过尝试从浏览器进行调试。我收到以下提到的错误:

未知的网络方法数据。
 参数名称:methodName
 在System.Web.Script.Services.WebServiceData.GetMethodData(String methodName)
 在System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(对象发送者,EventArgs eventArgs)
 在System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
 在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值和已完成同步)

Has*_*ari 5

我正在做的是WebMethod从 aspx.vb调用。

下面是我WebMethod在 Default.aspx.vb 中的语法:

<System.Web.Services.WebMethod()> _
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)> _
Run Code Online (Sandbox Code Playgroud)

您还需要添加以下导入:

Imports System.Web.Services  
Imports System.Web.Script.Services
Run Code Online (Sandbox Code Playgroud)