在Asp.net中,客户端脚本调用Web service的2种方法。

当今时代,是Ajax横行的时代,下面介绍2种在Asp.net里调用Web service的方法。

一.微软的Ajax脚本调用Web service

第一步:添加ScriptManger控件,注意一定要设置正确的引用URL,否则后面调试的时候会很郁闷。

    <asp:ScriptManager ID="ScriptManager1" runat="server">         <Services>             <asp:ServiceReference Path = "~/WebService.asmx" />         </Services>     </asp:ScriptManager>

第二步:在Web Config文件中正确的添加节点

      1. 在System.web下添加下列内容

<httpHandlers>      

  <remove verb="*" path="*.asmx"/>      

  <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,     Version=3.5.0.0, Culture=neutral,  PublicKeyToken=31BF3856AD364E35"/>      

 <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions,  Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>      

 <add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>    

</httpHandlers>    

<httpModules>      

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>    

</httpModules>

    2. 如果后期调用Web service失败,错误信息“Request format is unrecognized for URL unexpectedly ending in  '/HelloWorld'.”,那么你就需要再在System.Web下面添加下面的节点。

    <webServices>

      <protocols>
        <addname="HttpGet"/>
        <addname="HttpPost"/>
      </protocols>
    </webServices>
 

第三步:在后台添加一个.asmx文件

这里需要注意2点:

    1. 需要在类上添加属性[System.Web.Script.Services.ScriptService],以允许使用脚本调用Web service;

    2.需要在类里德方法上添加属性[WebMethod],以至于ajax请求能够找到你要调用的方法。

       例如:

    [System.Web.Script.Services.ScriptService]    

    public class ExampleWebService : System.Web.Services.WebService

    {

        [WebMethod(EnableSession = true) ]//红字标注的是你可以在这个方法里使用Session数据        

        public string HelloWorld()         {             return "Hello World";         }

   }

第四步:JS脚本调用Web service

<script type="text/javascript" language="javascript">        

function Succsess(result)

{            

  alert(result);        

}        

function Fail(ex)

{            

  alert("failure:" + ex.get_message());        

 }

ExampleWebService .HelloWorld(Success, Fail);

</script>

二. 使用jquery的ajax函数调用Web serivce

用该方法时,需要按照上面的第二步设置web config文件,按照第三步建立service类。

Jquery调用Web service,注意默认时请求类型是POST

$.ajax({                 

  type: "POST",                 

  async: false,                 

  url: "WebServices/ValidateService.asmx/CheckFirstName",                 

  data: { ParaName: ParaValue},                 

  success: function (result) {                 

    var Result = true;                 

    if ($.browser.mozilla) {                        

      Result = result.firstChild.firstChild.textContent;                  

     }                 

     else if ($.browser.safari) {                        

       Result = result.firstChild.firstChild.nodeValue;                  

    }                  

    else {                        

      Result = result.text;                  

    }

});

posted @ 2012-03-13 01:34  jixuguo  阅读(385)  评论(0)    收藏  举报