【问题标题】:"Unknown Web Method" Ajax Error“未知的 Web 方法”Ajax 错误
【发布时间】:2019-01-08 03:37:26
【问题描述】:

错误函数中返回的数据是:Unknown web method IncrementTable(pageNumber)。参数名称:方​​法名。我确信我在某个地方犯了一个小错误,但希望第二双眼睛能帮助我找到它。 :)

我的 .aspx 页面如下所示:

<%@ Page Title="TouchStoneTV" Language="VB" AspCompat="True" AutoEventWireup="True" Debug="True" EnableEventValidation="True" ValidateRequest="False" Trace="False" EnableViewState="True" %>

<%@ Import Namespace="System.Web.Services" %>
<%@ Import Namespace="System.Web.Script.Services" %>

<WebMethod()> _
Public Shared Function IncrementTable '(ByVal PageNumber As Integer)

Dim PageNumber As Integer = 1
For x As Integer = 0 To Math.Ceiling(RowsPerPage/CellsPerRow)

    Dim r As TableRow = New TableRow
    'If (x Mod CellsPerRow) = 0 Then r = New TableRow
    r.Attributes.Add("style","text-align:center;height:200px;width:50%;")

    Dim c1, c2 As New TableCell
    c1.Attributes.Add("style","border:1px solid #000;")
    c2.Attributes.Add("style","border:1px solid #000;")
    c1.Controls.Add(New LiteralControl(PageNumber.ToString & "a : " & x.ToString))
    c2.Controls.Add(New LiteralControl(PageNumber.ToString & "b : " & x.ToString))
    r.Cells.Add(c1)
    r.Cells.Add(c2)

    MainContentTable.Rows.Add(r)

Next x

Return "test"

End Function

而 javascript 看起来像这样:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script type="text/javascript">

var pageNumber = 1;
//var webMethodUrl = '<%=ResolveUrl("~/Mobile.aspx/IncrementTable(pageNumber)") %>'; //alert(webMethodUrl); //this gets a 404-not found error
var webMethodUrl = 'Mobile.aspx/IncrementTable(pageNumber)'; 

$(document).ready(function () {
    $(window).scroll(function () {
        // Get the current vertical position of the scrollbar.  If it's equal to the height of the document minus the 
        // window height then go get some more data
        if ($(window).scrollTop() == $(document).height() - $(window).height()) {
            // Increment page number and set up the parameters
            pageNumber += 1;
            var params = "{'pageNumber': " + pageNumber + "}";

            // Async post back to the BindDataAsync code behind web method
            $.ajax({
                type: "POST",
                url: webMethodUrl,
                data: params,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (data) {alert('error: ' + data);},
                success: function (data) {alert('success');
                    //if (data != "") {
                        // Take the results from the web service method and append them to the table
                    //    $('#ProductsTable').append(data.d);
                    //}
                }
            });
        }
    });
});

</script>

【问题讨论】:

    标签: asp.net ajax vb.net webmethod


    【解决方案1】:

    去掉参数:

    var webMethodUrl = 'Mobile.aspx/IncrementTable';
    

    【讨论】:

    • 那我必须这样做:url: 'Mobile.aspx/IncrementTable', And...同样的错误。
    【解决方案2】:

    该方法配置为使用 httpget 调用,但您使用 POST 方法在 javascript 中调用。

    <WebMethod()> _
    <ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)> _
    Public Shared Sub IncrementTable(ByVal PageNumber As Integer)
      'code
    End Sub
    

    只需删除 UseHttpGet:=True 即可解决您的问题。或者更改 jquery ajax 代码以使用 GET 请求并将参数作为查询字符串传递

    【讨论】:

    • 我想知道这一点。我将该行更改为: _。而且...同样的错误。
    • 尝试从您的方法中删除 ScriptMethod 属性并保持 WebMethod 原样
    • 我收回了 - 你给了我一个线索......我正在更新这个问题。我不得不:1)删除 UseHttpGet,2)结果我正在刷新页面以测试它,并且 URL 最后有一些坏字符。
    • 现在它成功了,但是表格(在我上面显示的代码中)没有刷新。有什么想法吗?
    • (顺便说一句,如果您将其作为答案提交,我会将您的回复标记为正确)
    猜你喜欢
    • 1970-01-01
    • 2010-09-15
    • 2020-06-23
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 2014-11-11
    相关资源
    最近更新 更多