如何在ASP.NET用户控件中使用jQuery Ajax?

6
我想在ASP.NET用户控件中使用ajax。
    $.ajax({
        type: "POST",
        url: "*TCSection.ascx/InsertTCSection",
        data: "{id:'" + id + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var URL = msg.d;
            alert(URL);
        });

.cs 代码

[WebMethod]
public static string InsertTCSection(string id)
{
    string result = "deneme";
    return result; 
}

你是否遇到了一些错误? - Agustin Meriles
我在服务器端的函数中添加了断点,但它没有被触发。 - altandogan
你不能通过 jQuery 调用 UserControl 中保存的方法,因为在运行时没有 .ascx 这样的资源,它已经与所在的页面合并了。 - Manish Mishra
4个回答

5

您无法通过jquery调用用户控件中的方法。 因为 .ascx 控件不代表实际的 URL。

它们是用于嵌入在某个 ASP.NET 页面中的,因此它们在运行时不存在。 您可以创建一个单独的服务并在其中放置您的方法, 例如 Web 服务。

请参阅此处此处和许多其他资源。


3
我使用通用处理程序来解决这个问题。

1

尝试:

 $.ajax({
        type: "POST",
        url: "*TCSection.ascx/InsertTCSection",
        data: JSON2.stringify({ id: id}, //Your 2nd id is passing value but i dont know where its come from
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var URL = msg.d;
            alert(URL);
             }
         )};

在计算机科学中:
[WebMethod]
public static string InsertTCSection(string id)
{
    string result="deneme";
    return result; 
}

1
ID参数不重要,我想学习如何在.ascx中使用Ajax。有什么方法可以做到这一点吗? - altandogan

1
我认为你可能缺少了这个属性。
   [System.Web.Script.Services.ScriptService]

在你将类作为服务之前

[System.Web.Script.Services.ScriptService]
public class TCSection: System.Web.Services.WebService
{

    [WebMethod]
    public static string InsertTCSection(string id)
    {
    }
 }

或者另一个可能的原因是 Web 服务的路径不正确。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接