<?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; AJAX</title>
	<atom:link href="http://schotime.net/blog/index.php/tag/ajax/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>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>
