Ajax无法在asp.net和C#中调用webform的web方法

时间:2017-09-16 12:20:51

标签: c# jquery asp.net ajax asp.net-ajax

这是我的Ajax功能:

function createSalesInvoice() {
  var SalesInvoice = {};
    SalesInvoice.PaidAmount = $("#textbox1").val();
    SalesInvoice.RemainingAmount = $("#textbox2").val();       
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/AjaxRequestToServer.aspx/createSalesInvoice", //URI   
        data: '{SalesInvoice: ' + JSON.stringify(SalesInvoice) + '}',
        dataType: "json",
        success: function (data) {

            bootbox.alert("Item Added Successfully", function (e) { });

            GetSalesInvoiceItemsList();
            $("#hdfFlag").val("false");
        },
        error: function (xhr) {
        }
    });
}

我不能打电话给我[webmethod]。文本框中的所有值都返回正确。我想在表格中插入这些值。

[WebMethod]
        public static int createSalesInvoice(SalesInvoiceEntity SalesInvoice)
        {
            int i = SalesInvoiceManager1.createSalesInvoice(SalesInvoice);
            return i;
        }

2 个答案:

答案 0 :(得分:0)

 data: '{SalesInvoice: ' + JSON.stringify(SalesInvoice) + '}',

这应该是

data: JSON.stringify({'SalesInvoice':SalesInvoice }),

您的Javascript函数应如下所示,只更改了一行。 并且javscript对象名称更改为“SalesInvoiceJS”以便于区分。

   function createSalesInvoice() {
  var SalesInvoiceJS = {};

    SalesInvoiceJS.FinalAmount = $("#txtGrandTotal").val();
    SalesInvoiceJS.PaidAmount = $("#txtTotalPaid").val();
    SalesInvoiceJS.RemainingAmount = $("#txtTotalRemAmt").val();
   // alert(SalesInvoiceJS.TotalInvoiceValue); ----------------This alert is working fine and return all above values correct.
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/AjaxRequestToServer.aspx/createSalesInvoice", //URI   
        data: JSON.stringify({'SalesInvoice':SalesInvoiceJS }),
        dataType: "json",
        success: function (data) {

            bootbox.alert("Item Added Successfully", function (e) { });

            GetSalesInvoiceItemsList();
            $("#hdfFlag").val("false");
        },
        error: function (xhr) {
        }
    });
}

参考:From this place

答案 1 :(得分:0)

需要检查数据

中传递的参数
 SalesInvoice.FinalAmount = $("#txtGrandTotal").val();
 SalesInvoice.PaidAmount = $("#txtTotalPaid").val();
 SalesInvoice.RemainingAmount = $("#txtTotalRemAmt").val();

如果上面的参数是整数,如在webmethod参数 - (SalesInvoiceEntity SalesInvoice)中那么应该转换为int

SalesInvoice.FinalAmount = parseInt($("#txtGrandTotal").val());

并且值也不能为空而是0。

希望它有效。