Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello all,


I have created a web service and added a simple method which returns Sum of two numbers. Now I want to call this method from javascript. My web service is a separate project inside the Current solution and I have added the Web reference to it as well.
I have tried it like this:


<%@ Page="" Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<!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>
  <script language="JavaScript" type="text/javascript">
	function loadService() 
	{
		try 
		{	
			webServiceCaller.useService("http://localhost/WebService1/Service1.asmx", "Service1");
		}
		catch (e) 
		{
			alert(e);
		}
	}
	function handleResult(result) 
	{
		if (!result.error) 
		{
			alert("Success! Result: " + result.value);
		}
		else 
		{
			alert("Failed! Error: " + result.errorDetail.string);
		}
	}
	function myMethodAsync() 
	{
		callID = webServiceCaller.MyService.callService(handleResult, "GetSum", "Asynchronous Call");
	}
  </script>
</head>
<body id="webServiceCaller" style="behavior:url(webservice.htc)">
  <form id="form1" runat="server">
	<div>
	  <button id="callAsynch"  önclick="myMethodAsync()">Call Asynchronously</button>
	</div>
  </form>
</body>
</html>





But this code is not working. Please can any body help me what wrong with my this code or any simple Method to do this????
Any help would be appreciated.
tanweer,
Posted
Updated 18-Feb-11 2:08am
v4

Hi tanweer,

There is an issue with this code here:

<big style="font-size: 2em;">ö</big>nclick=“myMethodAsync()”
I've enlarged the first letter to make it easier to spot what has gone wrong. You should always debug your javascript code if there seems to be an issue with something not working in the browser. If you are using IE use "Developer Tools" and if you're using Firefox you cas take FireBug to debug you client side code.

In case you haven't figured it out yet: <big style="font-size: 2em;">o</big>nclick="myMethodAsync()"

Best Regards,

[Edit: It has been pointed out by Nishant Sivakumar that most of the quotes are also wrong. Fixed it in my sample.]
 
Share this answer
 
v4
Comments
Nish Nishant 18-Feb-11 9:07am    
Voted 5.

BTW the quote is wrong too - it's using a Unicode character instead of ".
Manfred Rudolf Bihy 18-Feb-11 9:37am    
Thanks for pointing that out.
How could I have missed that! :doh:
why not use AJAX, have a look at this article[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900