Example (using only client wwCallbackMethod class in wwScriptLibrary.js):
var Callback = new wwCallbackMethod(); Callback.serverUrl = "ClientCallbacks.ashx"; // *** parameters passed as values in second parm as array Callback.callMethod("GetCustomer",[10], CustomerList_Callback,OnError);
If you use the wwMethodCallback control this code reduces to a single line:
Proxy.GetCustomer(10,CustomerList_Callback,OnError);
ajaxJson("ClientCallbacks.ashx?Method=CallbackWithPost", "Name=Rick&Date=01/10/2008&Value=10", function(result) {alert("Message from Server:\r\n" + result) }, function(error) { alert(error.message); }, true );
Example (using wwScriptLibrary.js):
ajaxJson("ClientCallbacks.ashx?Method=TestMethod2", { Name:"Rick", Date: new Date(), Value:10 }, function(result) {alert("Message from Server:\r\n" + result) }, function(error) { alert(error.message); } );
Passing a raw POST buffer:
$.ajax( { url: "ClientCallbacks.ashx?Method=CallbackWithPost", data: { Name: "Rick", Date: new Date(), Value: 20 }, processData: true, // turn input into querystring style values dataType: "json", // result data type type: "POST", success: function(result) { alert("Message from Server:\r\n" + result.toLocaleString() ); }, error: function( xhr, message, ex) { alert("Ajax Error: " + message); } } );
This code uses an object that is parsed by jQuery.ajax into a raw POST buffer (processData=true which converts a map into POST variables).
Passing a JSON string (note the data must be a JSON string not an object/map!):
// *** Object to serialize var person = {Name:"Rick",Address:"32 Kaiea",Entered: new Date()}; // *** use wwScriptLibrary's embedded JSON based on Crockford's with mods // *** you can use whatever mechanism you choose to generate JSON however // *** although we recommend you use wwScriptLibrary because it's most compatible // *** with what the server expects. var jsonData = JSON.serialize( person ); $.ajax( { url: "ClientCallbacks.ashx?Method=TestDate", contentType: "application/json", data: jsonData, processData: false, // treat as json dataType: "json", type: "POST", success: function(result) { alert("Message from Server:\r\n" + result.toLocaleString() ); }, error: function( xhr, message, ex) { alert("Ajax Error: " + message); } } );
In this code the data sent is a JSON string. Here I use wwScriptLibrary and its JSON.serialize() function to convert and object into a JSON string that is then passed as a parameter, but you can use whatever mechanism or library you choose to arrive at a JSON string.