JQuery Ajax WebMethod返回一个对象

时间:2011-01-14 11:11:52

标签: c# asp.net jquery ajax webmethod

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

我试过返回一个JsonResult对象,实际的对象似乎什么都没有用!我没有使用MVC(不幸的是)。有没有办法可以从我的web方法返回一个可以由我的AJAX方法使用的对象?

这是我的JQuery AJAX方法的链接

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

谢谢!

1 个答案:

答案 0 :(得分:5)

我使用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; }