LA.NET [EN]

Jan 24

In previous versions of the AJAX extensions platform, you could use this control to call remote services exposed by a handler or a data service. The only restriction is that the server component needs to return data in JSON format because that”s the only format the control “understands”.

So, if you had a web service and didn”t want to build a data service, you just needed to set the serviceType property of the client control to Handler and use the correct url. I mention correct url because the handler used to process the request on the server was chosen after parsing the url of the request (for instance, in previous versions you could use something like webservicepath/js/methodname – the /js made it go through the rest handler).

Things have changed and now the Content-Type of the request is used to see if the current request should return json or soap. The problem with this is that my old code (which is based on the datasource control) stop working because the url  path hack i was using was no longer valid. So,. how can i call a web service (not a data service!) from a datasource control? easy: you need to handle the invokingRequest event of the WebRequestManager object and set the Content-Type header to application/json.

Here”s a small demo page that does just that:

 

<!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>
    <title>Untitled Page</title>
 </head>
<body>
  <script type=”text/javascript” src=”../MicrosoftAjax.debug.js”>
  </script>
  <script type=”text/javascript” src=”../Microsoft.Web.Resources.ScriptLibrary.PreviewScript.debug.js”>
  </script>
  <script type=”text/javascript”>
     Sys.Net.WebRequestManager.add_invokingRequest(
                function( sender, e ){
                  var req = e.get_webRequest();
                  req.get_headers()[“Content-Type”] = “application/json; charset=utf-8″
               }
    );
</script>
<script type=”text/xml-script”>
   <page xmlns:script=”http://schemas.microsoft.com/xml-script/2005″>
        <components>
           <dataSource id=”mySource” autoLoad=”true”
              serviceURL=”WebService.asmx/ObtemAlunos” serviceType=”Handler” /> 
       </components>
    </page>
 </script>
<input type=”button” value=”Mostrar dados” onclick=”handleInfo()” />
<script type=”text/javascript”>
    function handleInfo() {
         var source = $find(“mySource”);
         if( source != null ) {
            var tbl = source.get_data();
            for( var i = 0; i < tbl.get_LEN(); i++ ) {
                var row = tbl.getRow(i);
               alert( row.getProperty(“Nome”) + ” ” + row.getProperty(“Idade”) );
          }
        }
 }
</script>
</body>
</html>

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>