AJAX 调用:无效的 Web 服务调用,缺少参数值

AJAX call: Invalid web service call, missing value for parameter

本文关键字:调用 参数 Web 无效 AJAX 服务      更新时间:2023-09-26

我的 ajax 调用失败并显示有问题的消息。

[WebMethod(EnableSession = true)]
public static string UpdateTotalPrice(List<TicketPriceAjax> jsonTickets)
{
    // some server-side processing
    return "a string value";
}    

我试图传递的对象

public class TicketPriceAjax
{
  public string Fullname { get; set; }
  public string Price { get; set; }
  public string TicketDescription { get; set; }       
}

我的JavaScript在更改事件中被调用

$("select.combobox").change(function(e) {
    var ticketsArray = GetTicketsArray();        
    postTotalPrice(ticketsArray);
});
function GetTicketsArray() {
    var tickets = {};
    var prices = [];
    tickets.prices = prices;
    $("#divFullNames div.row").each(function () {
        var price = {
            "Fullname": $(this).find("input").val(),
            "Price": $(this).find("select").val(),
            "TicketDescription": $(this).find(":selected").text()             
        };
        tickets.prices.push(price);
    });
    return tickets;
};

function postTotalPrice(prices) {
    $.ajax({
        type: "POST",
        url: "Details.aspx/UpdateTotalPrice",
        data: JSON.stringify(prices),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        success: function (data) {
            UpdateTotalPrice(data);
        },
        error: function(data, textStatus, jqXHR) {
            console.log('error!', data, textStatus, jqXHR);
        }
    });
};

我做错了什么?

将数据部分更改为此

data: JSON.stringify({jsonTickets:prices})
Your webmethod expects a parameter with name jsonTickets so you need to specify parameter name in data part.

此外,您不需要分配票证.price只需创建一个数组并在其中推送对象并将其传递给Webmethod。通过这种方式,您可以创建一个列表,而不是将列表分配给其房地产价格,并且在网络方法中,您期望一个列表。

var tickets = [];
$("#divFullNames div.row").each(function () {
    var price = {
        "Fullname": $(this).find("input").val(),
        "Price": $(this).find("select").val(),
        "TicketDescription": $(this).find(":selected").text()             
    };
    tickets.push(price);

这会将列表传递给网络方法。