如何在asp.net用户控件中使用jQuery ajax?

alt*_*gan 6 asp.net ajax jquery

我想在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);
        });
Run Code Online (Sandbox Code Playgroud)

.cs代码

[WebMethod]
public static string InsertTCSection(string id)
{
    string result = "deneme";
    return result; 
}
Run Code Online (Sandbox Code Playgroud)

Man*_*hra 5

您不能通过jquery调用保存在usercontrol中的方法.因为.ascx控件不代表真实的url.

它们旨在嵌入到某些ASP.NET页面中,因此它们在运行时不存在.你可能会做的是创建一个单独的服务并将你的方法放在那里.像webservices.

看到这个,这个和许多其他人


Shr*_*ree 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);
             }
         )};
Run Code Online (Sandbox Code Playgroud)

在CS中:

[WebMethod]
public static string InsertTCSection(string id)
{
    string result="deneme";
    return result; 
}
Run Code Online (Sandbox Code Playgroud)