Jan 24

DataSource control: more breaking changes!

Posted in MS AJAX      Comments Off on DataSource control: more breaking changes!

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” “”>
<html xmlns=”” >
    <title>Untitled Page</title>
  <script type=”text/javascript” src=”../MicrosoftAjax.debug.js”>
  <script type=”text/javascript” src=”../Microsoft.Web.Resources.ScriptLibrary.PreviewScript.debug.js”>
  <script type=”text/javascript”>
                function( sender, e ){
                  var req = e.get_webRequest();
                  req.get_headers()[“Content-Type”] = “application/json; charset=utf-8”
<script type=”text/xml-script”>
   <page xmlns:script=”″>
           <dataSource id=”mySource” autoLoad=”true”
              serviceURL=”WebService.asmx/ObtemAlunos” serviceType=”Handler” /> 
<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”) );