2

Below is webmethod of my web form which is returning a List of data and it works fine:

[WebMethod]
public static List<SalesInvoiceFinalCalculationEntity> salesInvoiceFinalCalculaiton(string InvoiceNo)
{
    List<SalesInvoiceFinalCalculationEntity> list = new List<SalesInvoiceFinalCalculationEntity>();
    list = SalesInvoiceManager1.salesInvoiceFinalCalculaiton(InvoiceNo);
    return list;
}

But in below Ajax function, I can't retrieve the data. When I bind data to textbox in ajax success function, it displays Undefined text in Html textBox.

function salesInvoiceFinalCalculaiton() {        

    var InvoiceNo = $("#txt_InvoiceNo").val();
    $.ajax({
        async: false,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/AjaxRequestToServer.aspx/salesInvoiceFinalCalculaiton", //URI   
        data: "{InvoiceNo:'" + InvoiceNo + "'}",
        dataType: "json",
        success: function (data) {

            document.getElementById("txtinvoicevalue").value=(data.totalprice);
            document.getElementById("txtTotalDiscount").value = data.discountamt;
            document.getElementById("txtTotalTaxableValue").value = data.taxableamt;
            document.getElementById("txtTotalCGST").value = data.cgstamt;
            document.getElementById("txtTotalSGST").value = data.sgstamt;
            document.getElementById("txtGrandTotal").value = data.grandtotal;

        },
        error: function (xhr) {
            if (xhr.statusText == "Invalid Request") {
                sessionStorage.clear();
            }
        }
    });
}

Here is Data Layer and the stored procedure:

 public static List<SalesInvoiceFinalCalculationEntity> salesInvoiceFinalCalculaiton(string InvoiceNo)
        {
            try
            {


                List<SalesInvoiceFinalCalculationEntity> SalesInvoiceFinalCalculation = new List<SalesInvoiceFinalCalculationEntity>();

                DataSet ds = SqlHelper.ExecuteDataset(Util.ConnectionString, CommandType.StoredProcedure, "sp_salesInvoiceFinalCalculaiton",
                    new SqlParameter("@InvoiceNo", InvoiceNo));

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    SalesInvoiceFinalCalculationEntity list = new SalesInvoiceFinalCalculationEntity(dr);
                    SalesInvoiceFinalCalculation.Add(list);
                }
                return SalesInvoiceFinalCalculation;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

And this is my entity Class:

public class SalesInvoiceFinalCalculationEntity
    {
        public int InvoiceNo { get; set; }
        float totalprice { get; set; }
        float discountamt { get; set; }
        float taxableamt { get; set; }
        float cgstamt { get; set; }
        float sgstamt { get; set; }
        float grandtotal { get; set; }
        public SalesInvoiceFinalCalculationEntity() { }
        public SalesInvoiceFinalCalculationEntity(DataRow dr)
        {
            InvoiceNo = Convert.ToInt32(dr["InvoiceNo"]);
            totalprice = float.Parse(dr["totalprice"].ToString());
            discountamt = float.Parse(dr["discountamt"].ToString());
            taxableamt = float.Parse(dr["taxableamt"].ToString());
            cgstamt = float.Parse(dr["cgstamt"].ToString());
            sgstamt = float.Parse(dr["sgstamt"].ToString());
            grandtotal = float.Parse(dr["grandtotal"].ToString());
        }
    }

why is data is not received in success function!

Dinkar Veer
  • 69
  • 1
  • 13
  • Do you have any errors ? It is hitting your method ? – Mihai Alexandru-Ionut Sep 15 '17 at 11:58
  • @Alexandru-IonutMihai yes it hits method and method returns list if data. i dint get any errors. but it displays "Undefined" Text in textBox. Its not displays Actual Value In textbox. – Dinkar Veer Sep 15 '17 at 12:10
  • Have you tried success: function (data.d)? If memory serves, when using webmethods like you are the object is actually in 'data.d' and not just 'data' – Sean T Sep 15 '17 at 14:01

5 Answers5

1

First of all, using async: false it is a bad practice because it's freeze your window during to your request. Don't use it.

The issue is that you have to return a json object from your server-side method in order to receive response in success callback function of your ajax method.

[WebMethod]
public static string salesInvoiceFinalCalculaiton(string InvoiceNo)
{
        List<SalesInvoiceFinalCalculationEntity> list = new List<SalesInvoiceFinalCalculationEntity>();
        list = SalesInvoiceManager1.salesInvoiceFinalCalculaiton(InvoiceNo);
        return JsonConvert.SerializeObject(list);
}

Web requests work with json format.

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
1

Finally resolved it. I forgot to mentioned

Public

in

SalesInvoiceFinalCalculationEntity

entity all variables and document.getElementById("txtinvoicevalue").value=(data.d[0].totalprice); this should be instead of

document.getElementById("txtinvoicevalue").value=(data.totalprice);

halfer
  • 19,824
  • 17
  • 99
  • 186
Dinkar Veer
  • 69
  • 1
  • 13
0

I think the problem is, that you're trying to send a Java Class to your JavaScript file. You can only send simple data types numbers, letters. As I understand, you're trying to access the member variables of SalesInvoiceFinalCalculationEntity.

If that's the case, you need to send it as a JSON object and get the data like this and then parse it.

The idea behind AJAX is to make the experience for the user better by not freezing the website using asynchronous requests. By calling the AJAX call with

async: false

removes the idea behind AJAX. You could then simply make a normal call to the server.

Washipp
  • 1
  • 1
0

Use this:

https://www.newtonsoft.com/json

Serialize your list and return it as a string.

Then, in your javascript:

success: function (data) {
    data = JSON.parse(data);
    document.getElementById("txtinvoicevalue").value=(data.totalprice);
    document.getElementById("txtTotalDiscount").value = data.discountamt;
    document.getElementById("txtTotalTaxableValue").value = data.taxableamt;
    document.getElementById("txtTotalCGST").value = data.cgstamt;
    document.getElementById("txtTotalSGST").value = data.sgstamt;
    document.getElementById("txtGrandTotal").value = data.grandtotal;

},
Marco Salerno
  • 5,131
  • 2
  • 12
  • 32
0

Try success: function (data.d) rather than success: function (data). If I recall when using webmethods the return object is within data.d and not simply data.

Also, despite what other answers say. When using the [webmethod] attribute and jquery ajax, you do not have to convert your response object to json. It will do so automatically.

Sean T
  • 2,414
  • 2
  • 17
  • 23
  • it gives error to '.d' error-: expected'{' and expected';' – Dinkar Veer Sep 15 '17 at 14:17
  • it looks like you are returning a list, so you would also need to loop through your response object? What does your object look like in the browser dev tools? Can you post that please? – Sean T Sep 15 '17 at 14:24