2009-10-15 45 views
0

我在使用Firefox 客戶端調用.Net Web服務時遇到問題。一個簡單的例子對我來說就足夠了。如何使用JavaScript爲Firefox 3.0調用webservice

服務器端代碼是這樣的:

[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"; 
} 
} 

客戶端的.html代碼:

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

客戶端的.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; 
} 
} 

此Web服務在IE但不運行在Firefox上,因爲 webservice.htc(行爲文件)不適用於Firefox。我需要一個 javascript或類似的東西,我不能使用htc 文件...

回答

1

如果您計劃消耗在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; 
     } 
    } 
} 

客戶端HTML代碼:

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

客戶端的.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>