Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Web方法未调用_C#_Webmethod - Fatal编程技术网

C# Web方法未调用

C# Web方法未调用,c#,webmethod,C#,Webmethod,我在C#中使用Web方法,并通过Ajax调用该Web方法。但是没有调用web方法 我需要使用asp.net和c#方法动态加载图表。所以我使用web方法与客户端进行交互。请查看下面的代码。我的web方法没有调用 Aspx Page: <script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type = "text/javascript"> functi

我在C#中使用Web方法,并通过Ajax调用该Web方法。但是没有调用web方法

我需要使用asp.net和c#方法动态加载图表。所以我使用web方法与客户端进行交互。请查看下面的代码。我的web方法没有调用

Aspx Page:

<script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
    $.ajax({
        type: "POST",
        url: "CS.aspx/GetCurrentTime",
        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) {
    alert(response.d);
}
</script> 
</head>
<body style = "font-family:Arial; font-size:10pt">
<form id="form1" runat="server">
<div>
Your Name : 
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time" 
    onclick = "ShowCurrentTime()" />
</div>
</form>
</body>


“未定义”。需要您的帮助

jquery语法有问题。正确的语法应该如下所示。 请尝试下面更正的代码,并让我知道

<script src="scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
    $.ajax({
        type: "POST",
        url: "CS.aspx/GetCurrentTime",
        data: {name: $("#<%=txtUserName.ClientID%>").val()},
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response);
        }
    });
}
function OnSuccess(response) {
    alert(response);
}
</script> 

函数ShowCurrentTime(){
$.ajax({
类型:“POST”,
url:“CS.aspx/GetCurrentTime”,
数据:{name:$(“#”)val()},
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:一旦成功,
故障:功能(响应){
警报(响应);
}
});
}
函数OnSuccess(响应){
警报(响应);
}

您在网页中将其称为POST,但在控制器上它是GET(默认设置)


添加
[HttpPost]
属性以修复或更改ajax调用以获取(可能更好)。

您可以将以下行添加到web.config代码中吗?这些是针对reach GET和post协议的:

 <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>


您试过调试代码吗?它到达你的方法了吗?你的API的完整URL是什么?没有到达方法。完整URL D:\Dash\u 05\test\test。我的C#代码和Aspx代码在那里。请通过重写代码发布请您给出此更改的语法:
type:“post”
type:“GET”
我遵循的步骤是:Aspx page之后到In-App\u Start文件夹,RouteConfig文件它不工作。在文本框中输入值并单击按钮后,其显示“未定义”我已更改代码,请立即检查。将response.d更改为成功和失败方法中的响应
 <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>