<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Simplify calling ASP.NET AJAX services from jQuery</title>
	<atom:link href="http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/</link>
	<description>ASP.NET and AJAX code, ideas, and examples.</description>
	<lastBuildDate>Wed, 16 May 2012 23:02:53 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-46008</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Thu, 22 Sep 2011 16:28:57 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-46008</guid>
		<description>I don&#039;t use WCF.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t use WCF.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sabarish</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-46000</link>
		<dc:creator>Sabarish</dc:creator>
		<pubDate>Thu, 22 Sep 2011 10:31:56 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-46000</guid>
		<description>Can you help me with an example for consuming WCF service using jquery.

I&#039;ve trying a lot on this but no avail.

Regards,
Sab</description>
		<content:encoded><![CDATA[<p>Can you help me with an example for consuming WCF service using jquery.</p>
<p>I&#8217;ve trying a lot on this but no avail.</p>
<p>Regards,<br />
Sab</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David L.</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-45864</link>
		<dc:creator>David L.</dc:creator>
		<pubDate>Wed, 14 Sep 2011 03:31:03 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-45864</guid>
		<description>Ok, thanks for the explanation. I think I&#039;ve found a solution! I took another look at your complex types post again. This time I noticed a comment from valse (http://marcovalsecchi.blogspot.com/). Based on his post, I&#039;ve found that you can use a generic Dictionary as the type for the method parameter, instead of trying to define a class. Then you can access the parameters by index, similar to the Request collection.

Client:
&lt;pre lang=&quot;javascript&quot;&gt;
    // Iterate over datatable&#039;s aoData and build an
    //  object with named properties.
    var oData = {};
    $.each(aoData,function(){
        oData[this.name]=this.value;
    });

    //Then, iterate over addn&#039;l params, add to data
    $.each(codeParams,function(){oData[this.name]=this.value;});

    // Create DTO (receivable as Dictionary &lt;string,object&gt;
    var DTO = {&#039;p&#039;:oData};
    
    //POST json to server
    $.post(svcpath+method,JSON.stringify(DTO),fnCallback);
&lt;/pre&gt;
WebMethod:
&lt;pre lang=&quot;csharp&quot;&gt;
    [WebMethod(Description = &quot;Returns one page of data for the Drawings grid (DataTables.net format&quot;)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public DataTablesList getDrawingDT(Dictionary&lt;string, object&gt; p)
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        context.Response.ContentType = &quot;application/json&quot;;
         switch ((string) p[&quot;nodeType&quot;])
         {
          ...
&lt;/pre&gt;

 So, I&#039;m good now! :-) Thanks for your help, Dave, I really appreciate it!</description>
		<content:encoded><![CDATA[<p>Ok, thanks for the explanation. I think I&#8217;ve found a solution! I took another look at your complex types post again. This time I noticed a comment from valse (<a href="http://marcovalsecchi.blogspot.com/" rel="nofollow">http://marcovalsecchi.blogspot.com/</a>). Based on his post, I&#8217;ve found that you can use a generic Dictionary as the type for the method parameter, instead of trying to define a class. Then you can access the parameters by index, similar to the Request collection.</p>
<p>Client:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #006600; font-style: italic;">// Iterate over datatable's aoData and build an</span>
    <span style="color: #006600; font-style: italic;">//  object with named properties.</span>
    <span style="color: #003366; font-weight: bold;">var</span> oData <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>aoData<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        oData<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//Then, iterate over addn'l params, add to data</span>
    $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>codeParams<span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>oData<span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">value</span><span style="color: #339933;">;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Create DTO (receivable as Dictionary &lt;string,object&gt;</span>
    <span style="color: #003366; font-weight: bold;">var</span> DTO <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'p'</span><span style="color: #339933;">:</span>oData<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">//POST json to server</span>
    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span>svcpath<span style="color: #339933;">+</span>method<span style="color: #339933;">,</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>DTO<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>fnCallback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>WebMethod:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #000000;">&#91;</span>WebMethod<span style="color: #000000;">&#40;</span>Description <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Returns one page of data for the Drawings grid (DataTables.net format&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #000000;">&#91;</span>ScriptMethod<span style="color: #000000;">&#40;</span>ResponseFormat <span style="color: #008000;">=</span> ResponseFormat.<span style="color: #0000FF;">Json</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> DataTablesList getDrawingDT<span style="color: #000000;">&#40;</span>Dictionary<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span>, <span style="color: #FF0000;">object</span><span style="color: #008000;">&gt;</span> p<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span> context <span style="color: #008000;">=</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Web</span></span>.<span style="color: #0000FF;">HttpContext</span>.<span style="color: #0000FF;">Current</span><span style="color: #008000;">;</span>
        context.<span style="color: #0000FF;">Response</span>.<span style="color: #0000FF;">ContentType</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;application/json&quot;</span><span style="color: #008000;">;</span>
         <span style="color: #0600FF;">switch</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span><span style="color: #000000;">&#41;</span> p<span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;nodeType&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
         <span style="color: #000000;">&#123;</span>
          ...</pre></div></div>

<p> So, I&#8217;m good now! :-) Thanks for your help, Dave, I really appreciate it!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-45861</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Wed, 14 Sep 2011 02:44:57 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-45861</guid>
		<description>It&#039;s been a long time since I&#039;ve tried, but as long as your parameters are nullable (e.g. String, int?, bool?, etc), a WebMethod should handle varying parameters. If not, I&#039;m sure that you can use a DTO/Viewmodel with nullable parameters and only supply the ones you want to.

As for working with DataTables, you can use $.ajaxSetup to influence how its AJAX calls work. If what you&#039;ve got it working though, I wouldn&#039;t fix what isn&#039;t broken.

The primary reason that ASP.NET services force POST access is for security. When you allow GET requests, a third-party site can potentially make requests for your services using your users&#039; credentials (via the auth/session cookies that are automatically transmitted with any HTTP request) when they happened to unsuspectingly visit a malicious site. If the data you&#039;re displaying isn&#039;t particularly sensitive, it&#039;s probably safe to not worry about the GET vs. POST distinction. Many would argue that GET is more appropriate for idempotent read-only requests anyway - we only use POST with ASP.NET services to enable the nice JSON support.</description>
		<content:encoded><![CDATA[<p>It&#8217;s been a long time since I&#8217;ve tried, but as long as your parameters are nullable (e.g. String, int?, bool?, etc), a WebMethod should handle varying parameters. If not, I&#8217;m sure that you can use a DTO/Viewmodel with nullable parameters and only supply the ones you want to.</p>
<p>As for working with DataTables, you can use $.ajaxSetup to influence how its AJAX calls work. If what you&#8217;ve got it working though, I wouldn&#8217;t fix what isn&#8217;t broken.</p>
<p>The primary reason that ASP.NET services force POST access is for security. When you allow GET requests, a third-party site can potentially make requests for your services using your users&#8217; credentials (via the auth/session cookies that are automatically transmitted with any HTTP request) when they happened to unsuspectingly visit a malicious site. If the data you&#8217;re displaying isn&#8217;t particularly sensitive, it&#8217;s probably safe to not worry about the GET vs. POST distinction. Many would argue that GET is more appropriate for idempotent read-only requests anyway &#8211; we only use POST with ASP.NET services to enable the nice JSON support.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David L.</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-45858</link>
		<dc:creator>David L.</dc:creator>
		<pubDate>Tue, 13 Sep 2011 22:19:23 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-45858</guid>
		<description>I&#039;m using that approach most of my calls, again, much thanks to you!! However, in this case, the method is feeding the nifty Datatables grid plugin (http://www.datatables.net). The number of parameters sent by the plugin varies, depending on how many columns in grid, how many are searchable, sortable, etc. By default, it uses a GET to call the webmethod. I&#039;m successfully  using code ported from the plugin&#039;s PHP examples (see snippet below):
&lt;pre lang=&quot;csharp&quot;&gt;

        /*
         * Ordering
         */
        string sOrder = &quot;&quot;;
        if (context.Request[&quot;iSortCol_0&quot;] != null)
        {
            sOrder = &quot;ORDER BY  &quot;;
            for (int i = 0; i &lt; Int32.Parse(context.Request[&quot;iSortingCols&quot;]); i++)
            {
                string iSortCol = context.Request[&quot;iSortCol_&quot; + i.ToString()];
                string mDataProp = context.Request[&quot;mDataProp_&quot; + iSortCol];
                string sSortDir = context.Request[&quot;sSortDir_&quot; + i.ToString()];
                if (context.Request[&quot;bSortable_&quot; + iSortCol] == &quot;true&quot;)
                {
                    sOrder += aSortColumns[Array.IndexOf(aColumns, mDataProp)] + &quot; &quot; + sSortDir + &quot;, &quot;;
                }
            }
            sOrder = sOrder.TrimEnd(new char[] { &#039; &#039;, &#039;,&#039; });
        }
        if (sOrder == &quot;ORDER BY&quot; &#124;&#124; sOrder == &quot;&quot;) sOrder = &quot; ORDER BY &quot; + idcol + &quot; &quot;;
&lt;/pre&gt;

However, I&#039;m just wondering if there&#039;s a way to do similar using a POST, since that&#039;s ASP.NET default. But first, does it even matter? Is there anything &quot;bad&quot; about using GET instead of POST for a WebMethod? Secondly, is there any way to design a receiving class that ASP.NET can automagically map the parameters to, without knowing ahead of time what parameters will be sent?</description>
		<content:encoded><![CDATA[<p>I&#8217;m using that approach most of my calls, again, much thanks to you!! However, in this case, the method is feeding the nifty Datatables grid plugin (<a href="http://www.datatables.net" rel="nofollow">http://www.datatables.net</a>). The number of parameters sent by the plugin varies, depending on how many columns in grid, how many are searchable, sortable, etc. By default, it uses a GET to call the webmethod. I&#8217;m successfully  using code ported from the plugin&#8217;s PHP examples (see snippet below):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">&nbsp;
        <span style="color: #008080; font-style: italic;">/*
         * Ordering
         */</span>
        <span style="color: #FF0000;">string</span> sOrder <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>context.<span style="color: #0000FF;">Request</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;iSortCol_0&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            sOrder <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ORDER BY  &quot;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> Int32.<span style="color: #0000FF;">Parse</span><span style="color: #000000;">&#40;</span>context.<span style="color: #0000FF;">Request</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;iSortingCols&quot;</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #FF0000;">string</span> iSortCol <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Request</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;iSortCol_&quot;</span> <span style="color: #008000;">+</span> i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">string</span> mDataProp <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Request</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;mDataProp_&quot;</span> <span style="color: #008000;">+</span> iSortCol<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #FF0000;">string</span> sSortDir <span style="color: #008000;">=</span> context.<span style="color: #0000FF;">Request</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;sSortDir_&quot;</span> <span style="color: #008000;">+</span> i.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>context.<span style="color: #0000FF;">Request</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;bSortable_&quot;</span> <span style="color: #008000;">+</span> iSortCol<span style="color: #000000;">&#93;</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;true&quot;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    sOrder <span style="color: #008000;">+=</span> aSortColumns<span style="color: #000000;">&#91;</span>Array.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span>aColumns, mDataProp<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span> <span style="color: #008000;">+</span> sSortDir <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;, &quot;</span><span style="color: #008000;">;</span>
                <span style="color: #000000;">&#125;</span>
            <span style="color: #000000;">&#125;</span>
            sOrder <span style="color: #008000;">=</span> sOrder.<span style="color: #0000FF;">TrimEnd</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #FF0000;">char</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span> <span style="color: #666666;">' '</span>, <span style="color: #666666;">','</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>sOrder <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;ORDER BY&quot;</span> <span style="color: #008000;">||</span> sOrder <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #000000;">&#41;</span> sOrder <span style="color: #008000;">=</span> <span style="color: #666666;">&quot; ORDER BY &quot;</span> <span style="color: #008000;">+</span> idcol <span style="color: #008000;">+</span> <span style="color: #666666;">&quot; &quot;</span><span style="color: #008000;">;</span></pre></div></div>

<p>However, I&#8217;m just wondering if there&#8217;s a way to do similar using a POST, since that&#8217;s ASP.NET default. But first, does it even matter? Is there anything &#8220;bad&#8221; about using GET instead of POST for a WebMethod? Secondly, is there any way to design a receiving class that ASP.NET can automagically map the parameters to, without knowing ahead of time what parameters will be sent?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-45832</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Tue, 13 Sep 2011 15:14:02 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-45832</guid>
		<description>One of the nice things about the service approach is that ASP.NET will automatically deserialize and typecast input parameters if you send them in as JSON. Once you get comfortable with the approach, I think it&#039;s easier than parsing out the parameters manually. This post has an example of doing that with several input parameters: http://encosia.com/using-complex-types-to-make-calling-services-less-complex/</description>
		<content:encoded><![CDATA[<p>One of the nice things about the service approach is that ASP.NET will automatically deserialize and typecast input parameters if you send them in as JSON. Once you get comfortable with the approach, I think it&#8217;s easier than parsing out the parameters manually. This post has an example of doing that with several input parameters: <a href="http://encosia.com/using-complex-types-to-make-calling-services-less-complex/" rel="nofollow">http://encosia.com/using-complex-types-to-make-calling-services-less-complex/</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: David L.</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-45810</link>
		<dc:creator>David L.</dc:creator>
		<pubDate>Mon, 12 Sep 2011 19:55:32 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-45810</guid>
		<description>Hey Dave,
This is a great article and site, thanks! I&#039;ve been successfully using this pattern for my ajax calls. However, I&#039;m wondering if there is a way to access parameters through the HttpContext.Current.Request collection, in a POSTed, JSON-returning ASP.NET 2.0 WebMethod? This would be similar to the many PHP examples on the web that access parameters like &quot;$_REQUEST[&quot;parameter_blah&quot;]&quot;. It works fine if the method is set up for GET (UseHttpGet=true), but with a POST, the Request comes up empty. Any ideas?</description>
		<content:encoded><![CDATA[<p>Hey Dave,<br />
This is a great article and site, thanks! I&#8217;ve been successfully using this pattern for my ajax calls. However, I&#8217;m wondering if there is a way to access parameters through the HttpContext.Current.Request collection, in a POSTed, JSON-returning ASP.NET 2.0 WebMethod? This would be similar to the many PHP examples on the web that access parameters like &#8220;$_REQUEST["parameter_blah"]&#8220;. It works fine if the method is set up for GET (UseHttpGet=true), but with a POST, the Request comes up empty. Any ideas?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alkos333</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-42368</link>
		<dc:creator>alkos333</dc:creator>
		<pubDate>Tue, 22 Feb 2011 21:51:14 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-42368</guid>
		<description>It was also the annoying format of { Id: 1, Name: &#039;Blah&#039; } instead of just {1:&#039;Blash&#039;}.  I ended up using a Dictionary</description>
		<content:encoded><![CDATA[<p>It was also the annoying format of { Id: 1, Name: &#8216;Blah&#8217; } instead of just {1:&#8217;Blash&#8217;}.  I ended up using a Dictionary</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-42351</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Mon, 21 Feb 2011 19:10:28 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-42351</guid>
		<description>In the scheme of things, the __type variable is more of an eyesore than it is a serious problem.  Since it doesn&#039;t change throughout the collection, it lends itself well to HTTP compression (i.e. gzip or deflate) and doesn&#039;t increase the payload size very significantly.

If it bothers you, you can return an anonymous type to get rid of it:

&lt;pre lang=&quot;csharp&quot;&gt;public object GetPerson(int ID) {
  Person p = new Person(ID);
  
  return new {
    FirstName = p.FirstName,
    LastName = p.LastName,
    // Etc
  }
}&lt;/pre&gt;

For collections, you can use LINQ to objects to pull out an IEnumerable collection of anonymous objects.</description>
		<content:encoded><![CDATA[<p>In the scheme of things, the __type variable is more of an eyesore than it is a serious problem.  Since it doesn&#8217;t change throughout the collection, it lends itself well to HTTP compression (i.e. gzip or deflate) and doesn&#8217;t increase the payload size very significantly.</p>
<p>If it bothers you, you can return an anonymous type to get rid of it:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">object</span> GetPerson<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> ID<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
  Person p <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Person<span style="color: #000000;">&#40;</span>ID<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #000000;">&#123;</span>
    FirstName <span style="color: #008000;">=</span> p.<span style="color: #0000FF;">FirstName</span>,
    LastName <span style="color: #008000;">=</span> p.<span style="color: #0000FF;">LastName</span>,
    <span style="color: #008080; font-style: italic;">// Etc</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>For collections, you can use LINQ to objects to pull out an IEnumerable collection of anonymous objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alkos333</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-42349</link>
		<dc:creator>alkos333</dc:creator>
		<pubDate>Mon, 21 Feb 2011 18:43:24 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-42349</guid>
		<description>JavaScriptSerializer works great on native and generic list/collections.  However, what&#039;s the suggested method for sending complex types back to the client?  I&#039;m using asmx service which by default utilizes JavaScriptSerializer.  When I try to send back a list of complex types, JSON ends up being cluttered with __type variables, etc.

Should I use a library like JSON.NET on the server side to serialize the complex type object and pass it back to the client using a string return type on the service method?</description>
		<content:encoded><![CDATA[<p>JavaScriptSerializer works great on native and generic list/collections.  However, what&#8217;s the suggested method for sending complex types back to the client?  I&#8217;m using asmx service which by default utilizes JavaScriptSerializer.  When I try to send back a list of complex types, JSON ends up being cluttered with __type variables, etc.</p>
<p>Should I use a library like JSON.NET on the server side to serialize the complex type object and pass it back to the client using a string return type on the service method?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alkos333</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-40003</link>
		<dc:creator>alkos333</dc:creator>
		<pubDate>Sun, 03 Oct 2010 20:49:27 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-40003</guid>
		<description>For example, List are returned as 
 { &quot;d&quot; : [ {  &quot;Key&quot; : &quot;blah&quot;, &quot;Value&quot; : &quot;foo&quot;}, { &quot;Key&quot; : &quot;foo&quot;, &quot;Value&quot; : &quot;foo&quot;}] }

instead of { [ blah : foo }, { blah3 : foo }] }</description>
		<content:encoded><![CDATA[<p>For example, List are returned as<br />
 { &#8220;d&#8221; : [ {  "Key" : "blah", "Value" : "foo"}, { "Key" : "foo", "Value" : "foo"}] }</p>
<p>instead of { [ blah : foo }, { blah3 : foo }] }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alkos333</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-40001</link>
		<dc:creator>alkos333</dc:creator>
		<pubDate>Sun, 03 Oct 2010 18:28:28 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-40001</guid>
		<description>It&#039;s not to have the ability to use UriTemplate(s).  If you use WebSerivceHostFactory, you can&#039;t really use those.</description>
		<content:encoded><![CDATA[<p>It&#8217;s not to have the ability to use UriTemplate(s).  If you use WebSerivceHostFactory, you can&#8217;t really use those.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-40000</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Sun, 03 Oct 2010 18:20:23 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-40000</guid>
		<description>There are two built-in JSON (de)serializers in .NET 3.5+.  JavaScriptSerializer is what ASMX uses. DataContractJsonSerializer is what WCF uses.  I prefer JavaScriptSerializer because it&#039;s easier to use and deserializes DateTime/Enum input types more flexibly.

I&#039;m not sure why you&#039;d go out of your way to abandon the automatic JSON handling that WCF and ASMX offer though. If you reinvent that wheel, you&#039;re basically just writing an HttpHandler. Why use WCF at all?</description>
		<content:encoded><![CDATA[<p>There are two built-in JSON (de)serializers in .NET 3.5+.  JavaScriptSerializer is what ASMX uses. DataContractJsonSerializer is what WCF uses.  I prefer JavaScriptSerializer because it&#8217;s easier to use and deserializes DateTime/Enum input types more flexibly.</p>
<p>I&#8217;m not sure why you&#8217;d go out of your way to abandon the automatic JSON handling that WCF and ASMX offer though. If you reinvent that wheel, you&#8217;re basically just writing an HttpHandler. Why use WCF at all?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: alkos333</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-39999</link>
		<dc:creator>alkos333</dc:creator>
		<pubDate>Sun, 03 Oct 2010 15:50:28 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-39999</guid>
		<description>That makes sense.

So if wanted to set up a WCF service that didn&#039;t use ASP.NET Ajax Endpoint, I would instead use a  basicHttpBinding with  behavior and some serializer on the server side like Atlas, AjaxPro or JSON.NET.  When doing it this way, the return types on all service methods would always be string, right?  What would the mime-type be set to then?  

On the client side then, all I&#039;d have to do is set the dataType to &quot;json&quot; and it&#039;ll do all the work for me</description>
		<content:encoded><![CDATA[<p>That makes sense.</p>
<p>So if wanted to set up a WCF service that didn&#8217;t use ASP.NET Ajax Endpoint, I would instead use a  basicHttpBinding with  behavior and some serializer on the server side like Atlas, AjaxPro or JSON.NET.  When doing it this way, the return types on all service methods would always be string, right?  What would the mime-type be set to then?  </p>
<p>On the client side then, all I&#8217;d have to do is set the dataType to &#8220;json&#8221; and it&#8217;ll do all the work for me</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave Ward</title>
		<link>http://encosia.com/simplify-calling-asp-net-ajax-services-from-jquery/#comment-39998</link>
		<dc:creator>Dave Ward</dc:creator>
		<pubDate>Sun, 03 Oct 2010 04:26:58 +0000</pubDate>
		<guid isPermaLink="false">http://encosia.com/?p=888#comment-39998</guid>
		<description>In general, json2.js shouldn&#039;t be used for security.  Its JSON.parse catches some things, but is definitely not a panacea.  At the same time, if you have good control over what your service is feeding the browser, eval() is safe enough.

If you&#039;re using jQuery 1.4+ and aren&#039;t stripping the .d, neither the dataFilter or json2.js are necessary for parsing the incoming response.</description>
		<content:encoded><![CDATA[<p>In general, json2.js shouldn&#8217;t be used for security.  Its JSON.parse catches some things, but is definitely not a panacea.  At the same time, if you have good control over what your service is feeding the browser, eval() is safe enough.</p>
<p>If you&#8217;re using jQuery 1.4+ and aren&#8217;t stripping the .d, neither the dataFilter or json2.js are necessary for parsing the incoming response.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

