【问题标题】:How to call webservice in Javascript for Firefox 3.0如何在 Firefox 3.0 的 Javascript 中调用 Web 服务
【发布时间】:2009-10-15 10:09:32
【问题描述】:

我在使用 Firefox 调用 .Net Web 服务时遇到问题 客户。一个简单的例子对我来说就足够了。

服务器端代码是这样的:

[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 文件...

【问题讨论】:

    标签: javascript asp.net firefox asmx


    【解决方案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>
    

    【讨论】:

      【解决方案2】:

      您可以使用 jQuery ajax 调用,它们使调用 Web 服务变得容易。见这里:http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

      【讨论】:

        猜你喜欢
        • 2010-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-26
        相关资源
        最近更新 更多