简体   繁体   English

如何在Firefox 3.0中使用Javascript调用Web服务

[英]How to call webservice in Javascript for Firefox 3.0

I have a problem with calling .Net web services with a Firefox client. 我在使用Firefox客户端调用.Net Web服务时遇到问题。 A simple example will be enough for me. 一个简单的例子对我来说就足够了。

Server side code is like this: 服务器端代码是这样的:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}

Client side .html code: 客户端.html代码:

Hello World Denemesi<br />
type="text" disabled="disabled" /></td> 
value="Print"           onclick="print()"> </td>

Client side .js code: 客户端.js代码:

var callObject;
function init(){
service.useService( "Service1.asmx?WSDL","Service");
callObject = service.createCallOptions();
callObject.async = false;

}
function print(){
callObject.funcName = "HelloWorld";
var oResult = service.Service.callService(callObject );
if( !oResult.error )
{
edtHelloWorld.value = oResult.value;
}
}

This web service works on IE but doesn't run at firefox because webservice.htc (behaviour file) doesn't work for firefox. 该Web服务可在IE上运行,但不能在Firefox上运行,因为webservice.htc(行为文件)不适用于Firefox。 I need a javascript or something like that wihch I cann use instead of htc file... 我需要一个javascript或类似的东西来代替htc文件...

If you are planning to consume your web service in NET, I would suggests using ScriptService, The client API is easier and should be working on most browsers, see below for a sample: 如果您打算在NET中使用Web服务,我建议您使用ScriptService。客户端API更容易使用,并且应该可以在大多数浏览器上使用,请参见以下示例:

namespace XXX.Services 
{  
    [System.Web.Script.Services.ScriptService()]
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [ToolboxItem(false)] 
    public class Service1 : System.Web.Services.WebService 
    { 
        [WebMethod] public string HelloWorld() 
        { 
            return "Hello World"; 
        } 

        [WebMethod] public string Greet(string name) 
        { 
            return "Hello " + name; 
        } 
    }
}

Client side html code: 客户端html代码:

Hello World Denemesi
<button onclick="test1()">print</button>

Client side .js code: 客户端.js代码:

<script>
    function test1(){
         XXX.Services.HelloWorld(function(result){
            alert(result);//do something with the result
         });

         XXX.Services.Greet("John Cane",function(result){
            alert(result);
         });
    }
</script>

You could use the jQuery ajax calls, they make calling web services easy. 您可以使用jQuery ajax调用,它们使调用Web服务变得容易。 See here: http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/ 看到这里: http : //encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM