SIGN UP MEMBER LOGIN:    
ARTICLE

Calling Web Method Which Takes Object as an Input Parameter Using AJAX & JavaScript

Posted by Nagashyam Kumar Articles | AJAX in C# June 28, 2011
Here you will see how to call a Web Method which takes an object as an input parameter Using AJAX, The object is created using JavaScript and passed as an input parameter.
Reader Level:
 


Step 1: Create a class with a few properties, such as:

public class Customers
    {
        private int _intCustomerID;
        private string _strFirstName;
        private string _strAddress;
        private string _strDesignation;
        public int CustomerID{
            get{
                return _intCustomerID;
            }
            set{
                _intCustomerID = value;
            }
        }
        public string FirstName{
            get{
                return _strFirstName;
            }
            set{
                _strFirstName = value;
            }
        }
        public string Address{
            get{
                return _strAddress;
            }
            set{
                _strAddress = value;
            }
        }
        public string Designation{
            get{
                return _strDesignation;
            }
            set{
                _strDesignation = value;
            }
        }


Step 2: Add a web service and the ScriptService Attribute to the service.

Step 3: Include a web method which takes an object as an input parameter as in:

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
     [System.Web.Script.Services.ScriptService]
    public class Customer : System.Web.Services.WebService
    {
        [WebMethod]
        public string TestMethod(Customers cust)
        {
            return "Hi " + cust.FirstName + " you are living in " + cust.Address + " and your desc is " + cust.Designation;
        }
    }

Step 4: Create an aspx page and include a ScriptManager.

Step 5: Add a Services tag and asp:ServiceReference and in the path give the webservice url:

        <asp:ScriptManager ID="ScriptManager1" runat="server">
            <Services>
                <asp:ServiceReference Path="~/Customer.asmx" />
            </Services>
        </asp:ScriptManager>


Step 6: Create a JavaScript function to call the webmethod.

Step 7: Create a JavaScript object manually which is similar to a C# class:

        var cust = new Object();
        cust.CustomerID = 1;
        cust.FirstName = "Shyam";
        cust.Address = "Live in India";
        cust.Designation = "Software Developer";



Step 8: webmethod with the delegate with the following parameters:

  • First input parameters
  • Second function to be bind the values
  • Third function handles error
  • Last function handles timeout

function callwebservicewithobj()
      {
            var cust = new Object();
        cust.CustomerID = 1;
        cust.FirstName = "Shyam";
        cust.Address = "Live in India";
        cust.Designation = "Software Developer";

        // CALLINGWEBMETHODUSINGAJAX is name space of webservice
        // Customer = webservices name.
             CALLINGWEBMETHODUSINGAJAX.Customer.TestMethod(cust, BindData, ErrorHandler, TimeOutHandler);
        }


Step 9: Create a JavaScript bind function

function BindData (varResult)
        {
            alert(varResult);
        }

Step 10: Create a JavaScript ErrorHandler function:

function ErrorHandler(result)
        {
            var msg = result.get_exceptionType() + "\r\n";
            msg += result.get_message() + "\r\n";
            msg += result.get_stackTrace();
            alert(msg);
        }


Step 11: Create JavaScript TimeOutHandler function:

        function TimeOutHandler(result)
        {
            alert("Timeout :" + result);
        }

Step 12: Create a link in aspx which calls the js function:

<a href="javascript:callwebservicewithobj();">Call webservices with input parameters</a>

Login to add your contents and source code to this article
share this article :
post comment
 

Nice to see the post here .. looking very forward to see more from you.. Good start ..

Posted by Dhananjay Kumar Jun 28, 2011

Thanks for sharing...

Posted by Dinesh Beniwal Jun 28, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor