在 AJAX 调用时将对象发送到 WebMethod

本文关键字:WebMethod 对象 AJAX 调用 | 更新日期: 2023-09-27 17:56:00

我正在尝试将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; }
    }

为什么我总是收到"错误!"警报?

在 AJAX 调用时将对象发送到 WebMethod

您可以将数据发送到Web方法,下面给出了两种类型的方法

方法 1

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

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!")
 });
}

方法 2

使用 JSON.stringify(lottery) 将数组对象更改为字符串。

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: alert("error!")
 });
}

首先字符串化数据:

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 调用时都会触发。修改如上所示。