无法使用JQuery AJAX调用C#Webmethod

无法使用JQuery AJAX调用C#Webmethod,c#,javascript,jquery,ajax,webmethod,C#,Javascript,Jquery,Ajax,Webmethod,我正在尝试调用我创建的webmethod。我遇到的问题是ajax调用从不调用我的webmethod;这对我来说很奇怪,因为我在同一个文件中有另一个webmethod,它具有相同的返回类型和参数,可以在ajax调用的“url”定义中很好地引用 我的aspx文档包括以下内容: 文件名CS.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="_Default" %> <

我正在尝试调用我创建的webmethod。我遇到的问题是ajax调用从不调用我的webmethod;这对我来说很奇怪,因为我在同一个文件中有另一个webmethod,它具有相同的返回类型和参数,可以在ajax调用的“url”定义中很好地引用

我的aspx文档包括以下内容:

文件名CS.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="CS.aspx.cs" Inherits="_Default" %>
<script src="http://code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script>
    function ShowCurrentTime() {
    if(document.getElementById("txtUserName").value == "" && document.getElementById("app_name").value == ""){
        alert("One of the fields must be filled out");
        return 0;
    }


    $.ajax({
        type: "POST",
        url: "CS.aspx/getAppId",
        data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
   }
function OnSuccess(response) {
    var y = document.getElementById("test");
    y.innerHTML = response.d;

    }
    </script>

    <body>
    <form id="form1" runat="server">
        <div>
    <table style="background-color:#fcfcfc; width:30%;">
    <tr>
        <td><b>Application ID:</b> </td>
        <td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br /></td>
    </tr>

    <tr>
        <td><b>Application Name:</b></td>
        <td><asp:TextBox ID="app_name" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td><input id="btnGetTime" type="button" value="Search" 
            onclick = "ShowCurrentTime()" /></td>
    </tr>
    </table>
    <p id="test"></p>
    </body>
    </html>

我在return语句中放置了一个断点,只是为了确保在调试模式下甚至调用了该方法,并且没有运气。

如果要进行jQuery ajax调用,请确保有对jQuery的引用

此外,WebMethod的参数需要与传入的参数名匹配。参数为appName:

 $.ajax({
    type: "POST",
    url: "CS.aspx/getAppId",
    data: '{appName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    error: function (response) {
        alert(response.d);
    }
});
在代码中,存在不匹配


我还习惯于关闭div和form标记。

您需要将此服务放置在WCF或ASMX服务中,并为其创建JavaScript代理。请参阅ajax文章中的而不是
failure
属性,将其命名为
error
。您现在至少应该看到错误消息。此外,您发布到的URL是
CS.aspx/getappId
,但发布后的代码暗示了这一点(从
class\u默认值
文本)这就是您应该调用的
default.aspx
吗?@DavidG当我将
failure
属性更改为
error
并以与定义
failure
属性相同的方式对其进行定义时,唯一发生的事情是弹出一条空警报消息。我还尝试在匿名函数中以“hello”作为一个简单的“error”字符串发出警报,但即使在清除浏览器缓存后,仍然只有一个空白警报弹出。另外,webmethod在其中定义的文件名是CS.aspx.CS,我至少了解到,“url”属性被定义为\u文件的
name\u/文件中的\u webmethod\u的name\u
。非常感谢。考虑通过包含链接中的一些信息来扩展这个答案。不支持链接的答案可能会被标记和删除。问题是,我的Web方法的参数需要匹配我正在传入的参数名。非常感谢。很好,很高兴我能帮忙。
 $.ajax({
    type: "POST",
    url: "CS.aspx/getAppId",
    data: '{appName: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    error: function (response) {
        alert(response.d);
    }
});
[System.Web.Services.WebMethod]
public static string getAppId(string appName)
{
    return "this is a string";
}