2

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

10
  • try adding this [System.Web.Script.Services.ScriptService] on the class in which this method is located Commented May 17, 2016 at 12:52
  • What you get on browser console? Commented May 17, 2016 at 13:03
  • @guruprasad rao There is nothing that i get on the browser, when I debug and verify, the control doesn't pass to the method. Commented May 18, 2016 at 5:19
  • Then this purely WebMethod issue. Try searching on how you can call WebMethod through javascript.. You will get some solution.. Commented May 18, 2016 at 5:24
  • @GuruprasadRao I went through all the websites and i find no difference in the way they have mentioned and the way i have called the method. Commented May 18, 2016 at 6:36

3 Answers 3

1

I was facing similar issue today. In my case i had RouteConfig in App_start folder, So i solved my problem by commenting this line

//settings.AutoRedirectMode = RedirectMode.Permanent;

which was the cause of the problem.

also Your web method needs to be public and static.

something like

public static int MethodName()
{
    // your method
}
Sign up to request clarification or add additional context in comments.

2 Comments

i am not using MVC, so in my case this doesn't apply. Thank you. if any other suggestion it would be great
well i also wasn't using MVC, you can also use RouteConfig in Webforms, however for your issue please check Updated Answer
0

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;


            }

Comments

0

You have a tiny typo in your javascript. Remember that javascript is case-sensitive. You are passing in:

Type: "POST"

But in fact you should be passing in:

type: "POST"

If you use F12 on the Network tab, you will noticed that your current AJAX call is making a GET request, not a POST request.

Comments

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.