Erik Porter's Blog

Life and Development at Microsoft and Other Technology Discussions

News

    Hello Atlas

    So Wally posted how to get the JavaScript proxy to call a WebService from a page in Atlas.  It's probably obvious to most, but I went ahead and did a little Hello World app in Atlas.  Here are the steps to make a WebService call with Atlas.  Note: You should have Atlas installed.

    Create a new Atlas Web Project.  Add a new WebService called HelloWorld.asmx and add a WebMethod called HelloMe that takes a String called Name and returns a String.

    <WebMethod()> _
    Public Function HelloMe(ByVal Name As String) As String
        
    Return String.Format("Hello, {0}", Name)
    End Function

    Let's setup a TextBox and Button.

    <input type="text" id="txtName" name="txtName" />
    <input id="btnSayHello" type="button" value="Say Hello" />

    Now add the script tag to your page (in my case, Default.aspx).

    <script language="javascript" type="text/javascript" src="HelloWorld.asmx/js"></script>

    The /js that's added to the link to the WebService makes it generate a JavaScript proxy that's sent back.  It creates the proxy name as the name of the WebService.  In this case, HelloWorld.  Now call the WebMethod we created earlier.  We need it to call the WebMethod and when it completes, popup a dialog spitting out the returning String.

    <script language="javascript">
       
    function btnSayHello_onclick()
       
    {
           
    var txtName = document.getElementById('txtName');

           
    HelloWorld.HelloMe(txtName.value, onHelloMeCompleted);
       
    }

       
    function onHelloMeCompleted(result)
       
    {
           
    alert(result);
       
    }
    </script>

    So you can see I grab the value in the TextBox and pass it in to the WebMethod.  The second parameter is basically the method that should be called once the asynchronus call to the WebService is completed.  You can optionally also pass in a 3rd parameter for a method to be called if the WebService call times out.  Then in the complete method, we just display the results.  The results variable is the object being returned from the WebMethod.  This could be simple types like a String or Integer, all the way up to complex types like your own custom Classes.

    The last piece is to wire up the button to call the method.

    <input id="btnSayHello" type="button" value="Say Hello" onclick="btnSayHello_onclick();" />

    Type in your name in the TextBox and click the Button.  You'll notice without posting back, it calls back to the server and returns a string.

    This is super simple and doesn't even skim the surface of Atlas.  Just as a quick tip, you can use debug.dump(result); to see what's being returned.

    Comments

    Michael Schwarz said:

    See my blog at http://weblogs.asp.net/mschwarz/archive/2005/09/15/425220.aspx for the arguments can be used to get context and error callbacks.

    CIAO
    Michael
    # October 5, 2005 8:29 AM