【问题标题】:JQuery Ajax WebMethod return an objectJQuery Ajax WebMethod 返回一个对象
【发布时间】:2011-06-09 02:12:25
【问题描述】:

我的 C# 中有一个通过 Jquery ajax 方法调用的 web 方法。 Web 方法应该将一个对象返回给将用于填充的 Jquery。

我尝试返回一个 JsonResult 对象,即实际对象,但似乎没有任何效果!我没有使用 MVC(不幸的是)。有没有一种方法可以从我的 Web 方法返回一个可供我的 AJAX 方法使用的对象?

这是我的 JQuery AJAX 方法的链接

http://pastebin.com/tRSaY5rG
http://pastebin.com/WajXyPMM

谢谢!!

【问题讨论】:

  • 请把你完整的asmx源代码也贴出来
  • 哦,好的。对不起。将 $.ajax 调用中的错误更改为 error:function (xhr, ajaxOptions, throwedError){ alert(ajaxOptions); } 并检查你得到了什么错误

标签: c# asp.net jquery ajax webmethod


【解决方案1】:

我使用 JQuery 从数据库中获取结果并在页面上使用项目填充 UL。这是你要找的吗?

Javascript


        //Set up Approve Requests Page
        $("#approveRequests").bind('pageAnimationEnd', function () { getRequestList(); return false; });

        //Gets the list of requests
        function getRequestList() {
            // call server-side webmethod using jQuery
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Index.aspx/GetOrdersForApproving",
                data: "{ }", // send an empty object for calls with no parameters
                dataType: "json",
                success: displayRequests,
                failure: reportError
            });
        }

        //displays the requests in the ul
        function displayRequests(result) {
            // ASP.NET encapsulates JSON responses in a property "d"
            if (result.hasOwnProperty("d")) { result = result.d; }
            // iterate through player list and add info to the markup
            var ul = $("#requestsForApproval");
            for (i = 0; i 

" + result[i].Supplier + "

," + result[i].Description + "," + result[i].Value + ""); var li = $("" + "

" + result[i].OrderID + " - " + result[i].Supplier + "

" + "" + "" + result[i].Description + "" + " " + "" + "" + "" + "Quant: " + result[i].Quantity + "" + "" + "Price: " + result[i].UnitPrice + "" + "" + "Total: " + result[i].Value + "" + "" + "" + "" + " " + "
    Approve" + "Reject
" + "" + "" + ""); ul.append(li); }

ASPX


        /// 
        /// Gets a list of Request Lines
        /// 
        /// List of order lines
        [WebMethod]
        public static List GetOrdersForApproving()
        {
            try
            {
                List Lines = new List();
                foreach (Objects.Database.OrderLine oOrderLine in Objects.Database.OrderLine.GetLinesWaitingFor(StaticStore.CurrentUser.UserID, int.MinValue))
                {
                    Lines.Add(new iOrderLine(oOrderLine));
                }

                return Lines;
            }
            catch (Exception)
            {
                throw;
            }
        }

让我努力让它工作的代码是:

if (result.hasOwnProperty("d")) { result = result.d; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多