[英]JQuery code to call a c# method not working
I am calling a C# function from JQuery but it is giving error. 我从JQuery调用C#函数,但它给出了错误。
The JQuery function is written in ascx file and C# function to be called is written in that page's code behind. JQuery函数用ascx文件编写,要调用的C#函数写在该页面的代码后面。 I am loading the user control into an AJAX tab on tab change event.
我正在将用户控件加载到选项卡更改事件的AJAX选项卡中。
Googling, I got that I can not call C# function written in user control. 谷歌搜索,我得到了我不能调用用户控件编写的C#函数。 So I written a function in host page(ASPX) and this function is calling my user control function.
所以我在主页(ASPX)中编写了一个函数,这个函数正在调用我的用户控件函数。
But the AJAX request is some how failing, I don't know where. 但是AJAX请求有些失败,我不知道在哪里。 But interesting thing is I have kept a debugger in the error function and I checked the error object.
但有趣的是我在错误函数中保留了一个调试器,并检查了错误对象。
Status is 200, Status is OK, readyState is 4
状态为200,状态为OK,readyState为4
and ResponseText is the markup of the page. 和ResponseText是页面的标记。 This means the page is served but the kept the break point in the C# function.
这意味着页面已提供,但保留了C#函数中的断点。 It never hits.
它永远不会打。
I have no idea what's happening. 我不知道发生了什么。 Also this is the first time I am calling a C# function from front end.
这也是我第一次从前端调用C#函数。 I don't have detailed idea of what happens under the hood.
我没有详细了解引擎盖下发生的事情。 Please help.
请帮忙。 Below is the code: JQuery
下面是代码:JQuery
$(function () {
$(".hint ul li").click(function () {
// remove classes from all
$(".hint ul li").removeClass("active");
// add class to the one we clicked
$(this).addClass("active");
//Ankit J, Implement logic to call jquery
var Availability = this.childNodes[0].innerText.trim();
debugger;
$.ajax({
type: "POST",
url: "../Pages/MyPage.aspx/CallUCMethodFromJQuery",
data: "{'sAvailability' : 'Availability'}",
dataType: "json",
success: fnsuccesscallback,
error: fnerrorcallback
});
});
});
function fnsuccesscallback(data) {
alert("success-->" + data.d);
}
function fnerrorcallback(result) {
debugger;
alert("error-->"+result);
}
ASPX page's code behind function ASPX页面的代码隐藏功能
[System.Web.Services.WebMethod]
public void CallUCMethodFromJQuery(string sAvailability)
{
MyNamespace.UserControls.ControlName m = new UserControls.ControlName();
m.EditAvailabilityValue(sAvailability);
}
And then the UserControl's code behind 然后是UserControl的代码
public void EditAvailabilityValue(string sAvailability)
{
}
Sorry for not mentioning.... JQuery is in the User Control because the source of click event is a li element which is in the User Control. 很抱歉没有提及.... JQuery在用户控件中,因为click事件的来源是用户控件中的li元素。 Also the UserControl is in a folder UserControls and the host page is in the Pages folder and both these folders are in root folder.
此外,UserControl位于UserControls文件夹中,主机页面位于Pages文件夹中,这两个文件夹都位于根文件夹中。
Add contentType: "application/json; charset=utf-8"
attribute to your ajax call: 在您的ajax调用中添加
contentType: "application/json; charset=utf-8"
属性:
$.ajax({
type: "POST",
url: "../Pages/MyPage.aspx/CallUCMethodFromJQuery",
data: "{'sAvailability' : 'Availability'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: fnsuccesscallback,
error: fnerrorcallback
});
then change EditAvailabilityValue
method in the user control to static: 然后将用户控件中的
EditAvailabilityValue
方法更改为static:
public static void EditAvailabilityValue(string sAvailability)
{
}
and change CallUCMethodFromJQuery
method in the aspx page code behind to static
so it can be called using jQuery: 并将aspx页面代码中的
CallUCMethodFromJQuery
方法更改为static
以便可以使用jQuery调用它:
[System.Web.Services.WebMethod]
public static void CallUCMethodFromJQuery(string sAvailability)
{
MyNamespace.UserControls.ControlName.EditAvailabilityValue(sAvailability);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.