【问题标题】:how to use jQuery ajax with asp.net user controls如何将 jQuery ajax 与 asp.net 用户控件一起使用
【发布时间】:2014-01-08 08:06:07
【问题描述】:

text.ascx 代码:

<script type = "text/javascript">
    function ShowCurrentTime() {

        $.ajax({
            type: "POST",
            url: "TestAjax.aspx/GetCurrentTime",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        alert(response.d);
    }

<input type="checkbox" id='chkl' onclick=ShowCurrentTime();>';

text.ascx.cs 代码:

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    return "Hello " + name + Environment.NewLine + "The Current Time is: "
        + DateTime.Now.ToString();
}

如何在用户控件中调用jquery ajax。

【问题讨论】:

标签: jquery asp.net


【解决方案1】:

您不能在 UserControl 中调用 WebMethod。尝试在 WebPage 中使用 WebMethod。

看看Already answered here

【讨论】:

    【解决方案2】:

    你可以这样试试吗:

    $.ajax({
            type: "POST",
            url: "*name.ascx/method",
            data: { }   ,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                var URL = msg.d;
                alert(URL);
                 }
             )};
    

    希望这会有所帮助..

    【讨论】:

    • ShowCurrentTime(),它的功能我要调用usercontrol页面
    • 我认为您不能调用在 .ascx 用户控件中声明的页面方法。它必须位于 .aspx 页面或 .asmx 网络服务中。
    【解决方案3】:

    你可以试试这个代码。

    $.ajax({
        type: "POST",
        url: "/yourURL",
        dataType: "json",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            edata = $(data).find("string").text();
            alert(edata);
        },
        error: function(e){
                   alert('err');
        }
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2012-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多