如何使用Jquery Ajax调用Web方法

本文关键字:Web 方法 调用 Ajax 何使用 Jquery | 更新日期: 2023-09-27 18:06:51

我正在使用ajax呼叫[WebMethod],但它不起作用。我使用的代码是

ASPX页

$.ajax({
 type: "POST",
 url: '<%= ResolveUrl("~/CandidateManagement.aspx/GetCurrentDateTime") %>',   
 data: '{name: "' + document.getElementById("lbcb5").innerHTML + '" }', Value
 contentType: "application/json; charset=utf-8",
dataType: "json",
 success: OnSuccess,                                                  
 failure: function (response) {
 alert(response.d);
            }
});  

Aspx.cs page

[WebMethod]
    public static string GetCurrentDateTime(string name)
    {
        return "Hey! " + name + Environment.NewLine + "The Current Date & Time is: "
            + DateTime.Now.ToString();
    }

Thanks in advance

如何使用Jquery Ajax调用Web方法

你不需要在数据上加引号,这样做:

$.ajax({
 type: "POST",
 url: '<%= ResolveUrl("~/CandidateManagement.aspx/GetCurrentDateTime") %>',   
 data: {name: document.getElementById("lbcb5").innerHTML}, 
 contentType: "application/json; charset=utf-8",
 dataType: "json",
success: function(response)
{
alert(response)
},                                                  
failure: function (response) {
 alert(response.d);
            }
});  

您是否在aspx页面中添加了ScriptManager ?我认为你应该尝试添加属性EnablePageMethods="true"

试试这个:

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />

"当我使用ScriptManager显示错误时,只有一个ScriptManager实例可以添加到页面中。"当抛出这种类型的错误时,使用scriptmanager proxy

<ajax:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</ajax:ScriptManagerProxy>
--------------************DEFAULT.ASPX************--------------        
<script type="text/javascript">
                function YolAl(yol) {
                    deger='<%=ResolveUrl("~/'+ yol +'") %>'
                    return deger;
                }
            </script>
        *******************------USERCONTROL CALL JS-------************************
         $(window).scroll(function () {
                if ($(window).scrollTop() == $(document).height() - $(window).height()) {
                    deger += 1;
                    RepeaterFnc("BaglantiItem.ascx", deger, "BagListDiv", YolAl('Default.aspx/AddGadGets'));
                }
            });
            $(document).ready(function () {
                deger += 1;
                RepeaterFnc("DurumGuncelle.ascx", deger, "BagListDiv", YolAl('Default.aspx/AddGadGets'));
                RepeaterFnc("IlanlarTab.ascx", deger, "SolDivAlanId", YolAl('Default.aspx/AddGadGets'));
                RepeaterFnc("BaglantiItem.ascx", deger, "BagListDiv", YolAl('Default.aspx/AddGadGets'));
                RepeaterFnc("Top10TakipEt.ascx", deger, "SagDivFavoriId", YolAl('Default.aspx/AddGadGets'));
                RepeaterFnc("Top10Ilanlar.ascx", deger, "SolDivAlanId", YolAl('Default.aspx/AddGadGets'));
            });
            function TakipEtYenileFnc() {
                $('#SagDivFavoriId').html('');
                RepeaterFnc("Top10TakipEt.ascx", deger, "SagDivFavoriId", YolAl('Default.aspx/AddGadGets'));
            }
    function RepeaterFnc(usercontrol, deger, DivName,FuncName) {
        $('#ajaxloading').show();
        var NextId = deger;
        $.ajax({
            url: FuncName,
            type: "POST",
            data: "{usercontrol:'" + usercontrol + "', NextId:'" + NextId + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                OnSuccess1(msg, DivName);
            },
            error: function (msg) { OnError1(DivName); }
        });
        deger = parseInt(deger + 1);
    }
    function OnSuccess1(data, DivName) {
        $("#" + DivName).html($("#" + DivName).html() + data.d);
        $('#ajaxloading').hide();
    }
    function OnError1(divname) {
        return false;
        $("#" + divname).html("Error!");
    }