-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;
        }
15
  • did you add [WebMethod] attribute in your createSalesInvoice()? Commented Sep 16, 2017 at 12:25
  • @HamedJavaheri yes.. plz see edits in question my webmethod Commented Sep 16, 2017 at 12:27
  • have you checked your data property is proper? Commented Sep 16, 2017 at 12:33
  • are you using ScriptManager in your page? Commented Sep 16, 2017 at 12:34
  • @HamedJavaheri No. i dont use any ScriptManager Commented Sep 16, 2017 at 12:36

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

@DinkarVeer can you set default correct values to SalesInvoiceJS object instead of textbox values? and test function.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.