如何使用Jquery Ajax调用Web方法

时间:2014-06-04 09:45:33

标签: c# jquery asp.net ajax

我正在使用ajax来呼叫[WebMethod]。但它没有Works.Code我使用的是

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();
    }

提前致谢

4 个答案:

答案 0 :(得分:1)

您不需要数据引用,请执行以下操作:

$.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);
            }
});  

答案 1 :(得分:0)

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

添加它

试试这个:

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

答案 2 :(得分:0)

当我使用ScriptManager显示错误时,只能将一个ScriptManager实例添加到页面中。” 抛出此类错误时,请使用scriptmanager proxy

<ajax:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</ajax:ScriptManagerProxy>

答案 3 :(得分:-2)

--------------************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!");
    }