Calling a web service via javascript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • truezplaya
    New Member
    • Jul 2007
    • 115

    Calling a web service via javascript

    I've searched numerous places on the web and haven't been able to get my solution to work. I have set up an web service and all works perfectly. It a very simple one just one method that retruns hello world as a string

    Code:
    namespace MyWebApp
    {
        /// <summary>
        /// Summary description for FirstSharePointWebService
        /// </summary>
        [WebService(Namespace = "MyWebApp", Name = "FirstSharePointWebService")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        [System.Web.Script.Services.ScriptService]
        public class FirstSharePointWebService : System.Web.Services.WebService
        {
            [ScriptMethod]
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
        }
    I have then created a web part in SharePoint. In this web part all i am doing is adding a literal control to the page which is an input button. I am also adding another literal control which places the javascript on the page.

    Code:
    this.Controls.Add(new LiteralControl("
    <script language='javascript' type='text/javascript'>(function(){
    alert(1);
    service.useService('http://localhost:22222/12/TEMPLATE/LAYOUTS/AJAX/FirstSharePointWebService.asmx?WSDL','FirstSharePointWebService');
    }());
    function callService() {alert(2);
    MyWebApp.FirstSharePointWebService.HelloWorld(callComplete, callError);
    } 
    function callComplete(result){alert(result);}
    function callError(result){alert('ERROR');}
    </script>"));
                        
    this.Controls.Add(new LiteralControl("
    <input type='button' id='pow' value='Get Name' onclick='callService();' />"));
    When i click on the button i get the callError() output which indicates it's all hooked up happily enough but something is wrong in the callComplete method and i can't spot what!

    I am guessing it can't find the MyWebApp.FirstS harePointWebSer vice but i'm not sure.

    Any pointers tips trick would be massively appreciated. Cheers
    Truez
Working...