JQuery Ajax WebMethod returns an object

I have a web method in my C # that is being called via jQuery ajax. The web method should return the object back to jQuery, which will be used to populate it.

I tried to return the JsonResult object, the actual object and nothing works! I do not use MVC (unfortunately). Is there a way to return an object from my web method that 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!!

+4
source share
1 answer

I used jQuery to retrieve the results from the database and populate the UL on the elements page. 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 bothers me to get this work was:

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

Source: https://habr.com/ru/post/1335536/


All Articles