Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Everything functions correctly except:
JavaScript
$orders.append(newOrder.d);

It is supposed to add the
HTML
<li>name: somename, drink: somedrink</li>

to the
HTML
<ul class="orders">

in Default.aspx

Default.aspx
JavaScript
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var $orders = $('.orders');
            var $name0 = $('#<%= name1.ClientID %>');
            var $drink0 = $('#<%= drink1.ClientID %>');

            $('#<%= add_order.ClientID %>').on('click', function () {
                var order = {
                    name: $name0.val(),
                    drink: $drink0.val()
                };
                $.ajax({
                    type: 'POST',
                    url: 'Myorder.aspx/ordered',
                    data: JSON.stringify(order),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (newOrder) {
                        $orders.append(newOrder.d);
                    },
                    error: function () {
                        alert('Error in new order');
                    }
                });
            });
        });

Default.aspx.cs
C#
[System.Web.Services.WebMethod]
public static string ordered(string name,string drink)
{
    string data= "<li>name: " + name + ", drink: " + drink + "</li>";

    return data;
}
Posted
Updated 2-Dec-14 23:32pm
v3
Comments
DamithSL 3-Dec-14 4:52am    
what it the value you get as "newOrder.d"?
Teledextri 3-Dec-14 5:05am    
<li>name: somename, drink: somedrink</li>
Kornfeld Eliyahu Peter 3-Dec-14 5:32am    
Your code seems all right, so I suggest to debug what newOrder.d is!!! Check it!
Thanks7872 3-Dec-14 5:44am    
I don't know why you made call just to prepare the <li> when you already have name and drink values available.
Sinisa Hajnal 3-Dec-14 6:27am    
You already have all the values, save yourself server round trip and do it in javascript!

1 solution

Remove your ajax call first and simply place the html inside orders like
C#
 $(function () {
            var name = $('#<%= name1.ClientID %>').val();
            var drink = $('#<%= drink1.ClientID %>').val();
 
            $('#<%= add_order.ClientID %>').on('click', function () {
             var items="<li>name: " + name+ ", drink: " + drink+ "</li>";
                 $('.orders').html(items);
//if you want to append : $('.orders').append(items);
            });
        });

Hope this helps
 
Share this answer
 
v3
Comments
Teledextri 3-Dec-14 7:11am    
This flashes the order at button click but it does not save in the html ul the li.
Jameel VM 3-Dec-14 7:43am    
save means? r u save this html inside the service?
Teledextri 3-Dec-14 7:45am    
Yes save this into html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900