Sunday, January 9, 2011

'this' within JavaScript function

Sometime we want to reference within event handler function to the element on which the event occurred. Keyword this gives reference to the element.

Saturday, January 8, 2011

Microsoft AJAX returning JSON needs JavaScriptSerializer for deserialization

Microsoft AJAX  require JavaScriptSerializer to deserialize return object into JSON object. Use get-data() to extract object prior to deserialization.
Consider the code snip 
View
  <%using (Ajax.BeginForm("GetJSONGreeting", new AjaxOptions
                                                  {
                                                      OnComplete = "onComplete"
                                                  }))

...

 <script type="text/javascript">
           function onComplete(data) {
               var obj = Sys.Serialization.JavaScriptSerializer.deserialize(data.get_data());
               alert(obj.Message);
           }
    </script>
Controller
 public JsonResult GetJSONGreeting(string name)
        { 
            return Json( new {Message = string.Format("Hello {0} from JSON", name)});
        }