Online Earning Sources (Without Investment)

If you want to post, send your post on dotnetglobe@gmail.com .Put 'Title' as 'Subject'

Pages

Showing posts with label Javascripts. Show all posts
Showing posts with label Javascripts. Show all posts

Wednesday, June 27, 2012

Call web service using javascript


Step 1:Create one website


Step 2:Right click on root directory in Solution explorer and click on "Add Item"


Step 3: Select web Service and click on Add.




Step 4: Create Web Methods


using System;

using System.Collections.Generic;

using System.Web;

using System.Web.Services;

 

/// <summary>

/// Summary description for HelloWorld

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

//[System.Web.Script.Services.ScriptService]

public class HelloWorld : System.Web.Services.WebService {

 

    public HelloWorld () {

 

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

 

    [WebMethod]

    public string Hello() {

        return "Hello World";

    }

    [WebMethod]

    public string Welcome(string name)

    {

        return "Welcome! "+name;

    }

   

Step 5: Create Web form


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CallWebServiceByJS.aspx.cs"

    Inherits="CallWebServiceByJS" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

     <body>

 

    <form id="form1" runat="server">

    <asp:ScriptManager ID="ScriptManager1" runat="server">

    <Services>

        <asp:ServiceReference Path="~/HelloWorld.asmx" />

    </Services>

    </asp:ScriptManager>

<input type="text" name="Name" id="Name" />

<input type="button" name="CallWS" value="Call Web Service through Ajax" />

    </form>

</body>

</html>


Step 6: Add ScriptManager in it


Step 7: Add Serivce element in Script Manager

<asp:ScriptManager ID="ScriptManager1" runat="server">

    <Services>

        <asp:ServiceReference Path="~/HelloWorld.asmx" />

    </Services>

    </asp:ScriptManager>


Step 8:Open Webservice class "HelloWorld.cs" and uncomment below highlighted line to call web service methods from javascript


using System;

using System.Collections.Generic;

using System.Web;

using System.Web.Services;

 

/// <summary>

/// Summary description for HelloWorld

/// </summary>

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

[System.Web.Script.Services.ScriptService]

public class HelloWorld : System.Web.Services.WebService {

 

    public HelloWorld () {

 

        //Uncomment the following line if using designed components

        //InitializeComponent();

    }

 

    [WebMethod]

    public string Hello() {

        return "Hello World";

    }

    [WebMethod]

    public string Welcome(string name)

    {

        return "Welcome! "+name;

    }

   

}

Step 9:Add below script in web form and call web service method as highlighted code

Now we need some client-side functions, button to trigger Web Service request and a text box to provide the input for the Web Service:

  • SendRequest - this function will send asyncroneus request to the Web Service
  • OnComplete - this function will receive result from the Web Service
  • OnError - this function will be triggered if an error occures while executing Web Service
  • OnTimeOut - this function will be triggered if Web Service will not respond
  • Name- text box with the input for the Web Service
  • RequestButton - the button that triggers SendRequest function

    <script language="javascript">

        /*Using Script Manager*/

        function SendRequest() {

            HelloWorld.Welcome(document.getElementById('Name').value, OnComplete, OnError, OnTimeOut);

        }

        function OnComplete(arg) {

            alert(arg);

        }

        function OnTimeOut(arg) {

            alert("timeOut has occured");

        }

         function OnError(arg) {

            alert("error has occured: " + arg._message);

        }

      </script>


Step 10: Call method on onclick event of button

<input type="button" name="CallWS" value="Call Web Service through Ajax" onclick="return SendRequest()" />


Step 11: Run and check.



Happy Coding!!!


itworld