JQuery Ajax WebMethod 返回一个对象

发布于 2024-10-12 04:20:25 字数 410 浏览 7 评论 0原文

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

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

这是我的 JQuery AJAX 方法的链接

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

谢谢!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2024-10-19 04:20:25

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

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; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文