4

I have a web Method in my C# that is called via Jquery ajax method. The web method should return an object back to the Jquery which will be used to populate.

I have tried returning a JsonResult object, the actual object and nothing seems to work! I'm not using MVC (Unfortunately). Is there a way that I can return an object from my web method which can be used by my AJAX method?

here is the link for my JQuery AJAX method

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

Thanks!!

2
  • please post your full source code of asmx too Commented Jan 14, 2011 at 11:18
  • oh ok. sorry. Change Error in your $.ajax call to error:function (xhr, ajaxOptions, thrownError){ alert(ajaxOptions); } and check what error you are getting Commented Jan 14, 2011 at 11:41

1 Answer 1

5

I have used JQuery to get results from a database and populate a UL on a page with the items. Is this what you were looking for?

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

The bit that code me struggling to get this working was:

if (result.hasOwnProperty("d")) { result = result.d; }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.