无法使用jquery ajax调用调用aspx页面web方法?

时间:2014-06-27 08:45:18

标签: c# jquery asp.net ajax

这是我的ajax电话

$(document).ready(function () {

         $("#btnSubmit").click(function () {
             alert("I am in ?");
             $.ajax({
                 type: "POST",
                 url: "TestNew2.aspx/DisplayData",
                 data: "{}",
                 contentType: "application/x-www-form-urlencoded",
                 dataType: "text",
                 //success: function (msg) {
                 //    // Replace the div's content with the page method's return.
                 //    $("#btnSubmit").text(msg.d);
                 //    alert(msg.d);
                 //}


                 success: function (result, status, xhr) {
                     document.getElementById("lblOutput").innerHTML = xhr.responseText
                 },
                 error: function (xhr, status, error) {
                     alert(xhr.error);
                 }


             });
         });


     });

和我的网络方法[WebMethod] public static string DisplayData() { return DateTime.Now.ToString(); }

尝试在aspx页面上调用web方法时获取aspx页面。这是jQuery代码 任何人都可以指出可能出现的问题。因为Web方法没有被调用。

1 个答案:

答案 0 :(得分:0)

尝试喜欢

            $.ajax
                ({
                    url: " URL",

                    data: "{ 'name' : 'DATA'}",

                    dataType: "json",

                    type: "POST",

                    contentType: "application/json; charset=utf-8",                   

                    async: true,

                    dataFilter: function (data) { return data; },

                    success: function (data) 
                    {
                        alert(data);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert("error");
                    }
                });

OR

 jQuery.ajax({
    type: "POST",
    url: "Login.aspx/checkUserNameAvail",
    contentType: "application/json; charset=utf-8",
    data: "{'iuser':'" + userid + "'}",
    dataType: "xml",
    success: function (msg) {
        $(msg).find("Table").each(function () {
            var username = $(this).find('UserName').text();
            if (username != '') {
                //window.location.replace('/iCalendar.aspx');
                alert('This username already taken..');
                $("#reguser").val('');
                $("#reguser").focus();
            }
            else {
            }
        });
    },
    error: function (d) {
    }
});

.CS

[WebMethod(enableSession: true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
    public static string checkUserNameAvail(string iuser)
    {
        try
        {
            iCalendarClass iC = new iCalendarClass();
            DataSet ds = iC.checkUserNameAvail(iuser);
            return (ds.GetXml());
        }
        catch
        {
            return null;
        }
    }