<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Schotime.net &#187; jQuery</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://schotime.net/blog</link>
	<description>All Things .Net and Me</description>
	<lastBuildDate>Thu, 01 Jul 2010 14:42:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Dataset, Datatable to Json</title>
		<link>http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/</link>
		<comments>http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/#comments</comments>
		<pubDate>Sun, 27 Jul 2008 05:51:36 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Json]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/</guid>
		<description><![CDATA[After my previous posts about returning data to the client as a JSON object, I decided to have a go at returning a generic Datatable/Dataset. This however is not as easy as simple returning a Datatable in your code behind method or web service. There is a solution though and here it is.
If you break [...]]]></description>
			<content:encoded><![CDATA[<p>After my previous posts about returning data to the client as a JSON object, I decided to have a go at returning a generic Datatable/Dataset. This however is not as easy as simple returning a Datatable in your code behind method or web service. There is a solution though and here it is.</p>
<p>If you break a Datatable down it is really only a List of Dictionary objects so that&#8217;s how we&#8217;ll approach this problem. This is compatible with .NET 2.0 and above, with the Ajax installed.</p>
<p>I&#8217;d like to acknowledge <a href="http://www.trinet.co.uk/" target="_blank" onclick="pageTracker._trackPageview('/outgoing/www.trinet.co.uk/?referer=');">RichardD</a> for the idea.</p>
<p>Below is the solution.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Data;

<span style="color: blue">public static class </span><span style="color: #2b91af">JsonMethods
</span>{
    <span style="color: blue">private static </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;&gt;
        RowsToDictionary(<span style="color: #2b91af">DataTable </span>table)
    {
        <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;&gt; objs =
            <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;&gt;();
        <span style="color: blue">foreach </span>(<span style="color: #2b91af">DataRow </span>dr <span style="color: blue">in </span>table.Rows)
        {
            <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; drow = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();
            <span style="color: blue">for </span>(<span style="color: blue">int </span>i = 0; i &lt; table.Columns.Count; i++)
            {
                drow.Add(table.Columns[i].ColumnName, dr[i]);
            }
            objs.Add(drow);
        }

        <span style="color: blue">return </span>objs;
    }

    <span style="color: blue">public static </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; ToJson(<span style="color: #2b91af">DataTable </span>table)
    {
        <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; d = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();
        d.Add(table.TableName, RowsToDictionary(table));
        <span style="color: blue">return </span>d;
    }

    <span style="color: blue">public static </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; ToJson(<span style="color: #2b91af">DataSet </span>data)
    {
        <span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; d = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt;();
        <span style="color: blue">foreach </span>(<span style="color: #2b91af">DataTable </span>table <span style="color: blue">in </span>data.Tables)
        {
            d.Add(table.TableName, RowsToDictionary(table));
        }
        <span style="color: blue">return </span>d;
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>The static class JsonMethods exposes two public static methods and a private method. The public method ToJson() takes either a Dataset or a Datatable, and returns a Dictionary&lt;string,object&gt; object. The key to this class is the RowsToDictionary() method. </p>
<p>This method iterates through all the rows creating a dictionary entry for each column in the row using the column name as the key and storing the data value into the object. It then adds the Dictionary object to a List of Dictionary Objects and returns this to the ToJson() method. This Dictionary list is then added to another Dictionary object using the table name as the key. We&#8217;ll see how this all works together soon.</p>
<p>Lets have a look at the code behind now.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code">[System.Web.Script.Services.<span style="color: #2b91af">ScriptMethod</span>(ResponseFormat = <span style="color: #2b91af">ResponseFormat</span>.Json)]
[System.Web.Services.<span style="color: #2b91af">WebMethod</span>]
<span style="color: blue">public static </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: blue">object</span>&gt; getTable()
{
    <span style="color: blue">string </span>sql = <span style="color: #a31515">&quot;select user_name, active_indicator, create_date from users&quot;</span>;
    <span style="color: blue">string </span>connString = <span style="color: #a31515">&quot;database=db; server=localhost; user id=sa;&quot;</span>;

    <span style="color: blue">return </span><span style="color: #2b91af">JsonMethods</span>.ToJson(GetDataTable(sql, connString));
}

<span style="color: blue">private static </span><span style="color: #2b91af">DataTable </span>GetDataTable(<span style="color: blue">string </span>sql, <span style="color: blue">string </span>connString)
{
    <span style="color: blue">using </span>(<span style="color: #2b91af">SqlConnection </span>myConnection = <span style="color: blue">new </span><span style="color: #2b91af">SqlConnection</span>(connString))
    {
        <span style="color: blue">using </span>(<span style="color: #2b91af">SqlCommand </span>myCommand = <span style="color: blue">new </span><span style="color: #2b91af">SqlCommand</span>(sql, myConnection))
        {
            myConnection.Open();
            <span style="color: blue">using </span>(<span style="color: #2b91af">SqlDataReader </span>myReader = myCommand.ExecuteReader())
            {
                <span style="color: #2b91af">DataTable </span>myTable = <span style="color: blue">new </span><span style="color: #2b91af">DataTable</span>();
                myTable.TableName = <span style="color: #a31515">&quot;mydt&quot;</span>;
                myTable.Load(myReader);
                myConnection.Close();
                <span style="color: blue">return </span>myTable;
            }
        }
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>So what I have above is two static methods. One is GetTable which is the one we will access from the client. The other is a generic method for loading a results set into a Datatable. Note how I have set the TableName property. You will see why soon.</p>
<p>So using the <a href="http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/" target="_blank">jMsAjax plugin</a> as below will return the following JSON object.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code">$.jmsajax({
    type: <span style="color: #a31515">&quot;POST&quot;</span>,
    url: <span style="color: #a31515">&quot;Default.aspx&quot;</span>,
    method: <span style="color: #a31515">&quot;getTable&quot;</span>,
    data: {},
    dataType: <span style="color: #a31515">&quot;msjson&quot;</span>,
    success: <span style="color: blue">function</span>(data) {
        $(outputDT(data.mydt)).appendTo(<span style="color: #a31515">&quot;body&quot;</span>);
    }
});</pre>
</td>
</tr>
</tbody>
</table>
<p>Results (data):<br />
  </p>
<table cellspacing="0" cellpadding="2" width="640" border="1">
<tbody>
<tr>
<td valign="top" width="638">
<p><span style="font-size: 11px; font-family: courier new">{&quot;mydt&quot;:{&quot;user_name&quot;:&quot;000001&quot;,&quot;active_indicator&quot;:&quot;Y&quot;,&quot;create_date&quot;:&quot;\/Date(1170892765197)\/&quot;}, {&quot;user_name&quot;:&quot;000002&quot;,&quot;active_indicator&quot;:&quot;Y&quot;,&quot;create_date&quot;:&quot;\/Date(1170892765197)\/&quot;}]}</span> </p>
</td>
</tr>
</tbody>
</table>
<p>In the resulting data, the table name is the key to referencing the array of values. In this case we use &#8216;mydt&#8217; as the key. In the success function on the client request you may also notice an outputDT function. This is a little helper function which takes a JSON Datatable and returns a the results in a table. This is very useful for debugging. Here is the client side code.</p>
<table cellspacing="0" cellpadding="2" width="400" border="1">
<tbody>
<tr>
<td valign="top" width="400">
<pre class="code"><span style="color: blue">function </span>outputDT(dataTable)
{
    <span style="color: blue">var </span>headers = [];
    <span style="color: blue">var </span>rows = [];

    headers.push(<span style="color: #a31515">&quot;&lt;tr&gt;&quot;</span>);
    <span style="color: blue">for </span>(<span style="color: blue">var </span>name <span style="color: blue">in </span>dataTable[0])
        headers.push(<span style="color: #a31515">&quot;&lt;td&gt;&lt;b&gt;&quot;</span>+name+<span style="color: #a31515">&quot;&lt;/b&gt;&lt;/td&gt;&quot;</span>);
    headers.push(<span style="color: #a31515">&quot;&lt;/tr&gt;&quot;</span>);

    <span style="color: blue">for </span>(<span style="color: blue">var </span>row <span style="color: blue">in </span>dataTable)
    {
        rows.push(<span style="color: #a31515">&quot;&lt;tr&gt;&quot;</span>);
        <span style="color: blue">for </span>(<span style="color: blue">var </span>name <span style="color: blue">in </span>dataTable[row])
        {
            rows.push(<span style="color: #a31515">&quot;&lt;td&gt;&quot;</span>);
            rows.push(dataTable[row][name]);
            rows.push(<span style="color: #a31515">&quot;&lt;/td&gt;&quot;</span>);
        }
        rows.push(<span style="color: #a31515">&quot;&lt;/tr&gt;&quot;</span>);
    }            

    <span style="color: blue">var </span>top = <span style="color: #a31515">&quot;&lt;table border='1'&gt;&quot;</span>;
    <span style="color: blue">var </span>bottom = <span style="color: #a31515">&quot;&lt;/table&gt;&quot;</span>;  

    <span style="color: blue">return </span>top + headers.join(<span style="color: #a31515">&quot;&quot;</span>) + rows.join(<span style="color: #a31515">&quot;&quot;</span>) + bottom;
}</pre>
</td>
</tr>
</tbody>
</table>
<p>So as you can see, its now very easy to return a Datatable or Dataset as a JSON object ready for use on the client.</p>
<p>Hope this is as useful for you as it is for me.</p>
<p>Cheers,<br />
  <br />Schotime </p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/07/27/dataset-datatable-to-json/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>jQuery Plugin for ASP.net Ajax (jMsAjax)</title>
		<link>http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/</link>
		<comments>http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 13:25:49 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/</guid>
		<description><![CDATA[After my recent post on jQuery, JSON and dates in asp.net I decided that there must be a better solution so I set out writing my first jQuery plugin. And after a few hours I had it working to my delight.
It not only accepts a raw JSON object as input for method parameters, but safely [...]]]></description>
			<content:encoded><![CDATA[<p>After my recent post on <a href="http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/">jQuery, JSON and dates in asp.net</a> I decided that there must be a better solution so I set out writing my first jQuery plugin. And after a few hours I had it working to my delight.</p>
<p>It not only accepts a raw JSON object as input for method parameters, but safely parses and stringify&#8217;s the object using Crockford&#8217;s implementation (<a href="http://www.json.org/js.html" onclick="pageTracker._trackPageview('/outgoing/www.json.org/js.html?referer=');">http://www.json.org/js.html</a>). The syntax of the call doesn&#8217;t change if it is a &#8220;POST&#8221; or a &#8220;GET&#8221; as it is all handled inside the plugin. It also returns Dates without a problem.</p>
<p>I have add a Home page for the plugin at <a href="http://schotime.net/jMsAjax.aspx">http://schotime.net/jMsAjax.aspx</a> and also added it to the plugins pages on the jQuery website (<a title="http://plugins.jquery.com/project/jMsAjax" href="http://plugins.jquery.com/project/jMsAjax" onclick="pageTracker._trackPageview('/outgoing/plugins.jquery.com/project/jMsAjax?referer=');">http://plugins.jquery.com/project/jMsAjax</a>).</p>
<p>Here is the basic syntax:</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code">$.jmsajax({
    type: <span style="color: #a31515;">"POST"</span>,
    url: <span style="color: #a31515;">"jMsAjax.aspx"</span>,
    method: <span style="color: #a31515;">"getTime"</span>,
    data: { date_in: <span style="color: blue;">new </span>Date() },
    success: <span style="color: blue;">function</span>(data) {
        $(<span style="color: #a31515;">"#div"</span>).html(String(data));
    }
});</pre>
</td>
</tr>
</tbody>
</table>
<p>You can find more information, a demo and the download at <a href="http://schotime.net/jMsAjax.aspx">http://schotime.net/jMsAjax.aspx<br />
</a><strong>Update:</strong> Just fixed the download page. Sorry for the inconvenience.</p>
<p>And at only 4kb + jQuery its significantly less size than MsAjax.</p>
<p>I hope this is as helpful to you as it is for me.</p>
<p>Schotime</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>jQuery, AJAX, ASP.NET and Dates</title>
		<link>http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/</link>
		<comments>http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 09:30:01 +0000</pubDate>
		<dc:creator>Schotime</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://schotime.net/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/</guid>
		<description><![CDATA[Just recently I discovered jQuery and I have to say&#8230;.I&#8217;m a huge fan already. So I decided to setup my first AJAX call through jQuery and call a page method. After some playing around I finally had it. Thanks also go out to www.encosia.com. Thanks Dave. The only problem was that I could not parse [...]]]></description>
			<content:encoded><![CDATA[<p>Just recently I discovered jQuery and I have to say&#8230;.I&#8217;m a huge fan already. So I decided to setup my first AJAX call through jQuery and call a page method. After some playing around I finally had it. Thanks also go out to <a href="http://www.encosia.com" onclick="pageTracker._trackPageview('/outgoing/www.encosia.com?referer=');">www.encosia.com</a>. Thanks Dave. The only problem was that I could not parse Dates.</p>
<p style="padding-left: 30px;"><strong>Updated 04/07/08: </strong>Please see my <a href="http://schotime.net/blog/index.php/2008/07/01/jquery-plugin-for-aspnet-ajax-jmsajax/" target="_blank">latest post</a> for a jQuery plugin that works perfectly with Asp.net. Dates and all.</p>
<p>So here I show you my solution which works pretty well and suits my needs. Maybe you might find it useful.</p>
<p>Firstly I&#8217;ll show you the aspx page and code behind use to make the AJAX call.<br />
<strong>ASPX Page:</strong></p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="color: blue;">&lt;</span><span style="color: #a31515;">html</span><span style="color: blue;">&gt;
&lt;</span><span style="color: #a31515;">head</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">title</span><span style="color: blue;">&gt;</span>My jQuery Test<span style="color: blue;">&lt;/</span><span style="color: #a31515;">title</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">script </span><span style="color: red;">src</span><span style="color: blue;">="jquery.min.js" </span><span style="color: red;">type</span><span style="color: blue;">="text/javascript"&gt;&lt;/</span><span style="color: #a31515;">script</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">script </span><span style="color: red;">type</span><span style="color: blue;">="text/javascript"&gt;
      </span>$(document).ready(<span style="color: blue;">function</span>() {
            $(<span style="color: #a31515;">"#mybutton"</span>).click(<span style="color: blue;">function</span>() {
                  $.ajax({
                      type: <span style="color: #a31515;">"POST"</span>,
                      url: <span style="color: #a31515;">"Default.aspx/jQueryTest"</span>,
                      contentType: <span style="color: #a31515;">"application/json; charset=utf-8"</span>,
                      data: <span style="color: #a31515;">"{ 'dt_in': "</span>+(<span style="color: blue;">new </span>Date()).toMSJSON()+<span style="color: #a31515;">" }"</span>,
                      success: <span style="color: blue;">function</span>(response) {
                        <span style="color: blue;">var </span>data = parseMSJSONString(response);
                        alert(data.d.name);
                        alert(data.d.dt);
                      }
                  });
             });
        });
    <span style="color: blue;">&lt;/</span><span style="color: #a31515;">script</span><span style="color: blue;">&gt;
&lt;/</span><span style="color: #a31515;">head</span><span style="color: blue;">&gt;
&lt;</span><span style="color: #a31515;">body</span><span style="color: blue;">&gt;
    &lt;</span><span style="color: #a31515;">form </span><span style="color: red;">id</span><span style="color: blue;">="form1" </span><span style="color: red;">runat</span><span style="color: blue;">="server"&gt;
    &lt;</span><span style="color: #a31515;">div</span><span style="color: blue;">&gt;
        &lt;</span><span style="color: #a31515;">input </span><span style="color: red;">type</span><span style="color: blue;">="button" </span><span style="color: red;">id</span><span style="color: blue;">="mybutton" </span><span style="color: red;">value</span><span style="color: blue;">="GetDate" /&gt;
    &lt;/</span><span style="color: #a31515;">div</span><span style="color: blue;">&gt;
    &lt;/</span><span style="color: #a31515;">form</span><span style="color: blue;">&gt;
&lt;/</span><span style="color: #a31515;">body</span><span style="color: blue;">&gt;
&lt;/</span><span style="color: #a31515;">html</span><span style="color: blue;">&gt;  </span></pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Code Behind:</strong></p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code"><span style="color: blue;">public partial class </span><span style="color: #2b91af;">_Default </span>: System.Web.UI.<span style="color: #2b91af;">Page
</span>{
    <span style="color: blue;">protected void </span>Page_Load(<span style="color: blue;">object </span>sender, <span style="color: #2b91af;">EventArgs </span>e)
    {
    }

    [System.Web.Script.Services.<span style="color: #2b91af;">ScriptMethod</span>(ResponseFormat = <span style="color: #2b91af;">ResponseFormat</span>.Json)]
    [System.Web.Services.<span style="color: #2b91af;">WebMethod</span>]
    <span style="color: blue;">public static </span><span style="color: #2b91af;">Test </span>jQueryTest(<span style="color: #2b91af;">DateTime </span>dt_in)
    {
        <span style="color: #2b91af;">Test </span>t = <span style="color: blue;">new </span><span style="color: #2b91af;">Test</span>() { name = <span style="color: #a31515;">"My Name"</span>, dt = dt_in.AddDays(5) };
        <span style="color: blue;">return </span>t;
    }

    <span style="color: blue;">public class </span><span style="color: #2b91af;">Test
    </span>{
        <span style="color: blue;">public string </span>name { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
        <span style="color: blue;">public </span><span style="color: #2b91af;">DateTime </span>dt { <span style="color: blue;">get</span>; <span style="color: blue;">set</span>; }
    }
}</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Note:</strong> I have not set a dataType setting of &#8216;json&#8217; in the Ajax call to prevent the JSON string being evaluated into a JSON object before I can parse it. Also note that the name of the parameter passed in through the JSON string data must be the same as the static WebMethod parameter name. In this case &#8220;dt_in&#8221;.</p>
<p>So what we have above is a simple input button with the value of &#8220;GetDate&#8221; which is going to send the current date from JavaScript to the WebMethod, add 5 days to the Date, then add it to a custom class called Test and return an instance of it. At that point the Name and Date will be alerted to the screen, all without a PostBack.</p>
<p>Now usually this would not work however I have created to handy scripts to make it pretty seamless. One to convert a JavaScript Date object to a Date String that ASP.NET will accept, and the other to parse the JSON string returned into a JavaScript Date. Here are those two functions.</p>
<table border="1" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="400" valign="top">
<pre class="code">Date.prototype.toMSJSON = <span style="color: blue;">function </span>() {
      <span style="color: blue;">var </span>date = <span style="color: #a31515;">'"\\\/Date(' </span>+ <span style="color: blue;">this</span>.getTime() + <span style="color: #a31515;">')\\\/"'</span>;
      <span style="color: blue;">return </span>date;
};

<span style="color: blue;">function </span>parseMSJSONString(data)
{
    <span style="color: blue;">try </span>{
        <span style="color: blue;">var </span>newdata = data.replace(
            <span style="color: blue;">new </span>RegExp(<span style="color: #a31515;">'"\\\\\/Date\\\((-?[0-9]+)\\\)\\\\\/"'</span>, <span style="color: #a31515;">"g"</span>)
                        , <span style="color: #a31515;">"new Date($1)"</span>);
        newdata = eval(<span style="color: #a31515;">'('</span>+newdata+<span style="color: #a31515;">')'</span>);
        <span style="color: blue;">return </span>newdata;
    }
    <span style="color: blue;">catch</span>(e) { <span style="color: blue;">return null</span>; }
}</pre>
</td>
</tr>
</tbody>
</table>
<p>The first of these two functions toMSJSON extends the Date object by adding the toMSJSON method to it. By doing this any Date object created in JavaScript can be converted to its MS JSON equivalent ready to be sent via Ajax.</p>
<p>The second one is a plain method used to parse the JSON string returned by the WebMethod to a JSON string that is &#8216;evaled&#8217; and made a proper JSON object. Thereby making data.d.dt in the example above a Date object.</p>
]]></content:encoded>
			<wfw:commentRss>http://schotime.net/blog/index.php/2008/06/19/jquery-ajax-aspnet-and-dates/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
