UPDATE - fixed the date parsing because I originally used Date(318326400000) which instantiates the CURRENT Date in Javascript which means we need to use new Date(318326400000) instead. I also setup C# to return DateTime(1980, 2, 2) instead of returning the UNIX EPOCH 1970-01-01T00:00:00:00000.
GOAL
To support the ASP.NET's DateTime JSON serialization in jQuery's ajax() method. ASP.NET serializes the DateTime response as "\/Date(-19312300000)\/" and we want jQuery to automatically parse that as a native Javascript Date() object. This outlines the simplest approach to creating an ASP.NET web service in C#. This also shows an example of a Hashtable being returned by the ASP.NET webservice.
The solution to making jQuery recognize this ASP.NET DateTime JSON serialization is to harness the jQuery.ajax.dataFilter functionality since version 1.2.6 of jQuery.
It is important to note that the example Web Service I provide returns a Hashtable data type which is not fully supported by the Web Services functionality even in .NET 3.5. Case in point, this function will work perfectly when invoking it as a webservice through AJAX and the JSON serialization, but will fail when you attempt to reflect/introspect via appending the "?wsdl" to the URL GET string in your browser. You'll see something along the lines of "Method services.echoArgs can not be reflected." because any data type that is of an IDictionary base type cannot be serialized in XML out of the box when using .NET Web Services. This example really just shows that a Hashtable can actually be used when using the JSON serialization format.
Step 1
Call the jQuery.ajax() method with the appropriate dataFilter function declaration in the options to pre-parse the ASP.NET DateTime object into a native Javascript object:
Step 3
Create simple services.asmx file that declares your web service functionality:
<%@ WebService Language="C#" class="services" %>
using System;
using System.Collections;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
[ScriptService]
public class services : WebService {
[WebMethod]
public Hashtable echoArgs (string arg1) {
Hashtable tmpp = new Hashtable();
tmpp["arg1"] = arg1;
tmpp["datetime"] = new DateTime(1980, 2, 2);
return (Hashtable) tmpp;
}
}
In the end the success function in the jQuery.ajax() should output the native Javascript Date.toString() via the alert popup. Prior to parsing the response from ASP it would appear as: