使用Java脚本调用Web服务方法 [英] Call a Web Service Method Using Java Script

查看:72
本文介绍了使用Java脚本调用Web服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我创建了一个Web服务并添加了一个简单的方法,该方法返回两个数字的和.现在,我想从javascript调用此方法.我的Web服务是Current解决方案中的一个单独项目,我也向其中添加了Web引用.
我已经这样尝试过:


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>






但是此代码无法正常工作.请任何人帮助我这段代码有什么问题或执行此操作的任何简单方法吗????
任何帮助将不胜感激.
tanweer,






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,

推荐答案

嗨tanweer,

此处的代码有问题:

<big style="font-size: 2em;">ö</big>nclick=myMethodAsync()
我放大了第一个字母,以便更容易发现问题所在.如果浏览器似乎无法正常运行,则应该始终调试您的javascript代码.如果您使用的是IE,请使用开发人员工具",如果您使用的是Firefox,则可以使用FireBug调试客户端代码.

如果您还没有弄清楚,请执行以下操作:<big style="font-size: 2em;">o</big>nclick="myMethodAsync()"

最好的问候,

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,


为什么不使用AJAX,请查看此 ^ ].
why not use AJAX, have a look at this article[^].


这篇关于使用Java脚本调用Web服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆