-1

Here is my Ajax function:

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) {
        }
    });
}

I can't call my [webmethod]. all values from textbox returns correct. I want to insert those values in table.

[WebMethod]
        public static int createSalesInvoice(SalesInvoiceEntity SalesInvoice)
        {
            int i = SalesInvoiceManager1.createSalesInvoice(SalesInvoice);
            return i;
        }
Dinkar Veer
  • 69
  • 1
  • 13

2 Answers2

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

This should be

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

Your Javascript function should be like below, Only one line is changed. And a javscript object name is changed to "SalesInvoiceJS" for easy difference only.

   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) {
        }
    });
}

Reference : From this place.

Sanket Patel
  • 1,130
  • 14
  • 25
  • @DinkarVeer can you set default correct values to SalesInvoiceJS object instead of textbox values? and test function. – Sanket Patel Sep 16 '17 at 13:10
  • You're going to have to learn how to use your browser's tools to debug. Learn how to look for JavaScript errors and monitor network traffic. Check your function is actually getting called etc. – mason Sep 16 '17 at 14:10
0

Need to check the parameter passed in data

 SalesInvoice.FinalAmount = $("#txtGrandTotal").val();
 SalesInvoice.PaidAmount = $("#txtTotalPaid").val();
 SalesInvoice.RemainingAmount = $("#txtTotalRemAmt").val();

If above parameters are in integer as In webmethod parameter - (SalesInvoiceEntity SalesInvoice) then should be converted as int by

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

and also value must not be blank rather 0.

Hope it works.

Komal
  • 304
  • 1
  • 7