Sunday, June 7, 2009

Sys.Net.WebServiceProxy.invoke wrapper for jQuery

So, after reading Encosia's blog post on using the jQuery library as an alternative to the ScriptManager's Sys.Net.WebServiceProxy.invoke method and wanting to rid myself of 300k worth of js required by the ScriptManager, I decided to try it out. I had some issues with the serialization of JSON objects other than direct parameters, but overall found the approach much more elegant.


However, I had a lot of legacy js that was using the WebServiceProxy.invoke method. The long term solution is to rewrite the code to reflect the change to jQuery, but as a short term solution, I wrote this little wrapper so basically you could just remove the ScriptManager, add a reference to the jQuery library, and just chug along.


     var Sys = {         Net: {             WebServiceProxy: {                 invoke: function invoke(path, methodName, useHttpGet, parameters, succeededCallback,                 failedCallback, userContext, timeout) {                     if (typeof parameters !== "string") {                         parameters = JSON.stringify(parameters);                     }                     $.ajax({                         type: "POST",                         url: path + "/" + methodName,                         data: parameters,                         contentType: "application/json; charset=utf-8",                         dataType: "json",                         error: function(result) { failedCallback(result); },                         success: function(result) { succeededCallback(result.d); }                     });                 }             }         }     }; 



Leave a Comment

Name (required)

Email optional

Home Page

Comments

RSS Feed
Blog Roll
Resources
Live Traffic Feed
Archives
Affiliates