【问题标题】:Unable to call aspx page web method using jquery ajax call?无法使用 jquery ajax 调用调用 aspx 页面 web 方法?
【发布时间】:2014-06-27 08:45:18
【问题描述】:

这是我的 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 代码 谁能指出可能出了什么问题。因为没有调用网络方法。

【问题讨论】:

    标签: c# jquery asp.net ajax


    【解决方案1】:

    试试喜欢

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

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

    【讨论】:

      猜你喜欢
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多