给aspx的JQuery AJAX提供了未知的方法

时间:2014-06-23 01:59:46

标签: javascript jquery

使用VS2013,下面的代码给出了"未知方法"。我试图将其简化为最简单的测试,我看了几个例子和答案,但是,我仍然无法弄明白。

新生成的

aspx文件似乎正确连接

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="JQAjaxTest.aspx.vb" Inherits="zz_Work_JQAjaxTest" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
             <input type="submit" value="JQAjaxASPXTest" 
                   onclick="JQAjaxASPXTest(); return false;"/>
        </div>
        <div>
            <span id ="testspan" style="display:none">"Hey, I'm here"</span>
        </div>
    </div>
    <script src="JScript.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive-ajax.min.js" type="text/javascript"></script>
    </form>
</body>
</html>

代码隐藏,新创建,断点点击Page_Load函数

Imports System.Web.Services
Partial Class zz_Work_JQAjaxTest
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    End Sub

    <System.Web.Services.WebMethod>
    Public Shared Function JQAJAXTest() As String
        Return "Passed the Test"
    End Function
End Class

javascript:似乎正常启动,然后在错误返回时出现Unknown Method错误

function JQAjaxASPXTest() {
    $.ajaxSetup({ cache: false });
    $.ajax({
        type: "POST",
        url: "JQAjaxTest.aspx/JQAJAXTest",
        data: {},
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (rtnMsg) {
            AJAX_Return_Sucess(rtnMsg)
        },
        error: function (request, status, error) {
            JQAjaxASPXTestError(request);
        }
    })
}
function JQAjaxASPXTestError(request) {
    alert(request.responseText)
}
function AJAX_Return_Sucess(rtnMsg) {
    alert(rtnMsg)
}

1 个答案:

答案 0 :(得分:0)

你错过了:

 <WebMethod()> _

在vb.net中,应该像这样调用web方法:

  <WebMethod()> _
 Public Shared Function DisplayData() As String
  Return DateTime.Now.ToString()
End Function

链接http://www.aspdotnet-suresh.com/2012/03/how-to-call-aspnet-page-methods-from.html可能会对您有所帮助。