Shekar.gvr Shekar.gvr - 6 months ago 23
jQuery Question

unable to call a c# method from javascript using ajax

I have a c# method which I would like to call on client side.
I used ajax calling to achieve this

function ValidateIfDuplicate()
{
debugger

var billtext = $("#ctl00_ContentPlaceHolder2_textBoxBillNumber").val();
var retailer= $("#ctl00_ContentPlaceHolder2_dropDownListRetailers").val();
var billdate = $("#ctl00_ContentPlaceHolder2_textBoxBillDate").val();

if (billtext == "")
{
alert("Bill Number cannot be left empty");
return false;
}
else if (retailer == 0) {
alert("Please select a Retailer");
return false;
}
else if (billdate == '') {
alert("Date cannot be left empty");
return false;
}
else if (billtext != '' && retailer != '' && billdate != '')
{


$.ajax({
Type: "POST",
url: "CAInvoiceEntry.aspx/ValidateDuplicateEntry",
contentType: "application/json; charset=utf-8",
data: { billtext1: billtext, billdate1: billdate, retailer1: retailer },
dataType: "json",
success: function (result) {
debugger
alert(result.d);
}
});

return true;
}
}


and this is my c# method

[System.Web.Script.Services.ScriptService]
public partial class CAInvoiceEntry: BaseClass
{
[WebMethod, ScriptMethod()]
public static int ValidateDuplicateEntry(string billtext1, string billdate1, string retailer1)
{
string validatebill = objCAInvoiceEntry.validatebilldate(textBoxBillNumber.Text, billdate1.ToString(), ViewState[AppConstants.UploadedBy].ToString(), dropDownListRetailers.SelectedValue);
if (validatebill == "1")
{

return 1;
}
else
return 0;
}
}


but the web method is not fired.
I have also tried using pagemethods.methodname() as an alternative(by registring the script with enablepagemethods=true) but with no effect.

If someone can guide me on where i am doing it wrong?
Just to be clear..
in the below image you can see the breakpoint execution, where the ajax call gets skipped.
enter image description here

Answer

There seems to be as issue with the way you are passing parameters through ajax. Try this.

if (billtext != '' && retailer != '' && billdate != '')
            {

               debugger

               $.ajax({
                   type: "POST",
                   url: "CAInvoiceEntry.aspx/ValidateDuplicateEntry",
                   data: "{ billtext1:" + "'" + billtext + "'" + ", billdate1:" + "'" + billdate + "'" + ", retailer1:" + "'" + retailer + "'" + "}",
                   contentType: "application/json; charset=utf-8",
                   dataType: "json",
                   async: true,
                   cache: false,
                   success: function (msg) {
                       if (msg == "1")
                                       alert("Already exists");
                                   else
                                       alert("valid");
                   },
                   error: function (msg) {
                       debugger;
                       alert(msg);
                   }
               })
               return false;


            }