我如何获得json对象 [英] How do I get the json object

查看:56
本文介绍了我如何获得json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加到li列表但是我在订单方面遇到了一些麻烦。



当我在按钮后输入order时单击警报显示为:

I am trying to add to the li list but I am having some trouble with "order".

When I input "order" after the button click the alert shows it as:

"[object Object]"



应为:


It should read:

{name: somename, drink: somedrink}





如何获得json对象?





How do I get the json object?

<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 $name = $('#<%= name1.ClientID %>');
        var $drink = $('#<%= drink1.ClientID %>');

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

</ul>
<h4>Add a Coffee Order</h4>
<p>Name:<asp:TextBox ID="name1" runat="server"></asp:TextBox></p>
<p>Drink:<asp:TextBox ID="drink1" runat="server"></asp:TextBox></p>
<asp:Button ID="add_order" runat="server" Text="Add!" />

推荐答案

function (){
var


orders =


' .order');
var


这篇关于我如何获得json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆