By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Calling a web service via javascript

100+
P: 115
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

Expand|Select|Wrap|Line Numbers
  1. namespace MyWebApp
  2. {
  3.     /// <summary>
  4.     /// Summary description for FirstSharePointWebService
  5.     /// </summary>
  6.     [WebService(Namespace = "MyWebApp", Name = "FirstSharePointWebService")]
  7.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  8.     [System.ComponentModel.ToolboxItem(false)]
  9.     // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  10.     // [System.Web.Script.Services.ScriptService]
  11.     [System.Web.Script.Services.ScriptService]
  12.     public class FirstSharePointWebService : System.Web.Services.WebService
  13.     {
  14.         [ScriptMethod]
  15.         [WebMethod]
  16.         public string HelloWorld()
  17.         {
  18.             return "Hello World";
  19.         }
  20.     }
  21.  
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.

Expand|Select|Wrap|Line Numbers
  1. this.Controls.Add(new LiteralControl("
  2. <script language='javascript' type='text/javascript'>(function(){
  3. alert(1);
  4. service.useService('http://localhost:22222/12/TEMPLATE/LAYOUTS/AJAX/FirstSharePointWebService.asmx?WSDL','FirstSharePointWebService');
  5. }());
  6. function callService() {alert(2);
  7. MyWebApp.FirstSharePointWebService.HelloWorld(callComplete, callError);
  8. function callComplete(result){alert(result);}
  9. function callError(result){alert('ERROR');}
  10. </script>"));
  11.  
  12. this.Controls.Add(new LiteralControl("
  13. <input type='button' id='pow' value='Get Name' onclick='callService();' />"));
  14.  
  15.  
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.FirstSharePointWebService but i'm not sure.

Any pointers tips trick would be massively appreciated. Cheers
Truez
Mar 6 '12 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.