在AJAX调用WebMethod上发送对象

时间:2016-07-08 18:59:26

标签: c# jquery json ajax webmethod

我正在尝试将JSON对象发送到C#方法,并返回另一个(也是JSON)对象。

该对象是用JavaScript创建的:

    lottery = {
        TotalValue: totalValue,
        Players: txtPlayers.value,
        TicketPrice: txtAmount.value,
        FirstPrize: prize,
        MyComission: myComission,
        MyRate: myRate,
        SellerComission: sellerComission,
        SellerRate: sellerRate
    }

然后通过AJAX发送:

function CreateLottery(lottery) {
$.ajax({
    type: 'POST',
    url: 'default.aspx/Create',
    data: lottery,
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data, status) {
        alert(status)
    },
    error: alert("error!")
 });
}

C#代码隐藏(我有断点,它永远不会到达这里):

    [WebMethod]
    public static object Create(Lottery lottery)
    {
        return new
        {
            foo = "bar",
        };
    }
    public class Lottery
    {
        public decimal TotalValue { get; set; }
        public decimal Players { get; set; }
        public decimal TicketPrice { get; set; }
        public decimal FirstPrize { get; set; }
        public decimal MyComission { get; set; }
        public decimal MyRate { get; set; }
        public decimal SellerComission { get; set; }
        public decimal SellerRate { get; set; }
    }

为什么我总是得到“错误!”提醒?

2 个答案:

答案 0 :(得分:0)

首先对数据进行字符串化:

var lottery = {
    TotalValue: totalValue,
    Players: txtPlayers.value,
    TicketPrice: txtAmount.value,
    FirstPrize: prize,
    MyComission: myComission,
    MyRate: myRate,
    SellerComission: sellerComission,
    SellerRate: sellerRate
}

function CreateLottery(lottery) {
$.ajax({
    type: 'POST',
    url: 'default.aspx/Create',
    data: JSON.stringify(lottery),
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function (data, status) {
        alert(status);
    },
    error: function () {
        alert("error!");
    }
 });
}

编辑:您的错误回调定义不正确,并且每次进行ajax调用时都会触发。修改如上所示。

答案 1 :(得分:0)

您可以将数据发送到网络方法,下面给出了两种方法

方法1

您可以在Web方法中传递每个属性值,如

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        select {
            background-color: red;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlTest" runat="server">
            <asp:ListItem Value="1">One</asp:ListItem>
            <asp:ListItem Value="2">Two</asp:ListItem>
            <asp:ListItem Value="3">Three</asp:ListItem>
        </asp:DropDownList>
        <asp:DropDownList ID="ddlTest2" runat="server">
            <asp:ListItem Value="4">Four</asp:ListItem>
            <asp:ListItem Value="5">Five</asp:ListItem>
            <asp:ListItem Value="6">Six</asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

方法2

使用function CreateLottery(lottery) { $.ajax({ type: 'POST', url: 'default.aspx/Create', data: {'TotalValue': totalValue, 'Players': txtPlayers.value,'TicketPrice': txtAmount.value, 'FirstPrize': prize, 'MyComission': myComission, 'MyRate': myRate, 'SellerComission': sellerComission, 'SellerRate': sellerRate}, contentType: 'application/json; charset=utf-8', dataType: 'json', success: function (data, status) { alert(status) }, error: alert("error!") }); } 将数组对象更改为字符串。

JSON.stringify(lottery)