从jquery AJAX调用ASPX方法

时间:2013-11-08 22:02:33

标签: jquery asp.net ajax

我正在尝试通过AJAX使用jQuery在单独的ASPX文件中调用方法。在使用相同的基本程序运行了一些教程之后,我仍然没有运气。

这是标记

<input class="myButton">
<div id="debug"></div>

jquery的

$(".myButton").click(function(e){       
    e.preventDefault();
    alert('go');   //this triggers just fine.



    $.ajax({
                     type: "POST",
                     url: "/functions.aspx/ServerSideMethod",
                     data: "{'param1': 'foo'}",
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     async: true,
                     cache: false,
                     success: function (msg) {
                         $('div#debug').text(msg.d); 
                     }
        });



});

和ASP驻留在我的functions.aspx文件(所有这些,完全未经编辑)

[WebMethod]
public static string ServerSideMethod(string param1)
{
    return "Message from server with parameter:"+param1;
}

目前,它似乎正在连接到页面。我得到的错误如下:

  

未知的Web方法ServerSideMethod。参数名称:methodName

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.ArgumentException:未知的Web方法   ServerSideMethod。参数名称:methodName

1 个答案:

答案 0 :(得分:1)

您的网址可能不正确。如果您呼叫的页面位于以下位置:

/a/b/yourpage.aspx

您的电话正在以下地方查找您的functions.aspx文件

/a/b/functions.aspx

确保网址路径正确。