Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I call multiple code behind method from JavaScript?

Currently I am using
But I think first second method is call simultaneously,
what I want, if first method is called successfully then
only second method should be called.

So how can I make this possible?

Code below:

JavaScript
function OnResetDataClick() {
      var UserEmail = document.getElementById("ContentPlaceHolder1_UserName1");
      var val = UserEmail.value;
      var usedata= "";

      var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
      if (reg.test(val) == false) {
      }
      else {
        
        var getPassword = {
          type: "POST",
          url: "GetData.aspx/GetDataMethod",
          data: "",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (response) {
            usedata= response.d;

          },
          error: function (result) {
            alert("Error");
          }
        };

        //Call the PageMethods
        $.ajax(getPassword);
       
      // Calling second Page Method
      if (usedata!= "") {
        var options = {
          type: "POST",
          url: "GetData.aspx/SendData",
          data: "{'toMailAddress':'" + val + "','usefulldata':'" + usedata+ "'}",
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (response) {
            var val1 = response.d;
            alert(val1);
          },
          error: function (result) {
            alert("Error in " + result);
          }
        };

        //Call the PageMethods
        $.ajax(options);
        // }
      }

    }
Posted
Updated 8-Nov-10 23:24pm
v3

wrap the call to your second page method in a function, call it SendResetMailMessage(). then in your success handler of your first page method call you can simply call your wrapped function:

success: function (response) {
  usedata= response.d;
  SendResetMailMessage();
}
 
Share this answer
 
Just use setTimeout to execute the methods asynchronous using a timeout.

setTimeout ("GetDataMethod();", 100);
setTimeout ("SendData();", 100);

function SendData()
{
 ... ajax call ...
}

function GetDataMethod()
{
 ... ajax call ...
}


Gopod luck!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900