C# Web Service and EXT JS

Initially i had trouble using a C# web service and extjs. This was due to .net automatically wrapping the JSON in a XML doc type declaration. The following web service gave me the correct response that ext can deal with.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true,XmlSerializeString = false)]   
     public List<User> getUserFriends() 
     { 
         List<User> users = new List<User>();
         try
         {
             using(SqlConnection_connection = new SqlConnection(_connectionstring))
             {
                 using(SqCommand cmd = new SqlCommand("spGetUserFriends",_connection))  
                 {  
                     cmd.CommandType = CommandType.StoreProcedure;
                     _connection.Open();
                     using(SqlDataReader reader = cmd.ExecuteReader())  
                     {
                         while(reader.Reader())
                         {  
                             users.Add(new User((int)reader["UserID"],(string)reader["UserName"]));  
                         }
                     }  
                 }    
             }  
             catch(SqlException e)
             {
                 Console.WriteLine(e);     
             }  
         return users;  
      }  




Then store which calls the web service:


 Ext.define("extjs.store.DropFriends",{ 
   extend:'Ext.data.Store',
   autoLoad: true, 
   storeId: 'dropFriends'  
   proxy: new Ext.data.HttpProxy({  
       type: 'ajax',
       url: 'AuthService.asmx/getUserFriends',
       headers: {Content-Type' : 'application/json'},
       reader: {  
           type: 'json',  
           root: 'd'  
       }
     }),  
     model:'extjs.model.User'  
     });


Finally a simple model  in extjs for the User Object:
 Ext.define("extjs.model.User",{ 
   extend:'Ext.data.Model,
   fields:['userID','userName'], 
   });

Powered by Blogger.