Calling asp.net Ajax.method from Javascript

AC 0 Reputation points
2023-06-22T01:41:36.23+00:00

I am having a default.js file in shared folder. Dynamically we will send aspx file name and function name and the parameters to call corresponding aspx page and method.


Default.js method

function ValidateParams(fromdate,Id)
{
 var exp=/^((((0?[13578])|(1[02]))[\/]((0?[1-9]|[0-2][0-9])|(3[01])))|(((0?[469])|(11))[\/]((0?[1-9]|[0-2][0-9])|(30)))|(0?[2][\/](0?[1-9]|[0-2][0-9])))[\/]\d{4}$/;
 var exp1=/^(\d{9})$/;
 if(exp.test(fromdate)&& exp1.test(Id)) 
 {
   CheckValidityofId("FormUI.CheckValidity",fromdate,Id)
 }
}

function CheckValidityofId(lookupFunction,fromdate,Id) {

    callback = function(response) {
    
        if (response.error) {
            alert(response.error);
        }
        else {
        
            if (response.value =="NotAssociated")
            {
            document.getElementById(MessageID).innerText="Number is not associated";
            }
            else
            {
            document.getElementById(MessageID).innerText="This is NOT eligible";
            }
            
            
        }
    }
    if (fromdate.length > 0 && Id.length >0) {
        eval(lookupFunction + "('" + fromdate + "','" + Id "'," + callback + ")");
    }
  return false;
}

Here is my FormUI.ascx.cs method

public partial class FormUI : UIView
{
 [Ajax.AjaxMethod()]
  public string CheckValidity(string FromDate, string Id)
  {
          // logic goes here
  }
}


Please correct me what's the wrong why the above code is not working. Its not hitting the 'CheckValidity' method in ascx.cs file. Even I tried with the below code but its's also not working


Please let me know what is missing.
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,206 Reputation points Microsoft External Staff
    2023-06-22T03:03:24.8+00:00

    Hi @AC,

    You can check the following points:

    (1) WebService Method.

    (2) The method should be static. Otherwise the method will not be called.

    (3) Two parametes that will passed from $.ajax() method.

    User's image

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Bruce (SqlWork.com) 81,976 Reputation points Volunteer Moderator
    2023-06-23T16:00:10.0566667+00:00

    you are using an old library. does a hard coded call work?

    is FormUI defined and does it have a method CheckValidity?

    you are using an old ajax library, how did you generate the FormUI object?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.