<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Derik Whittaker : jQuery</title><link>http://devlicio.us/blogs/derik_whittaker/archive/tags/jQuery/default.aspx</link><description>Tags: jQuery</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Typescript, jQuery and the ‘this’ context causing issues</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2013/04/12/typescript-jquery-and-the-this-context-causing-issues.aspx</link><pubDate>Fri, 12 Apr 2013 15:36:26 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:109644</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=109644</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=109644</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2013/04/12/typescript-jquery-and-the-this-context-causing-issues.aspx#comments</comments><description>&lt;p&gt;Today I was in the middle of porting some raw javascript over to TypeScript as well as moving inline javascript into separate classes.&amp;#160; In the middle of doing this I started to run into some really odd stuff.&amp;#160; Namely, the ‘this’ context in one of my methods was set to window, not to the parent object.&amp;#160; This was really weird and at first I thought maybe it had to do w/ Inheritance and Typescript (this class implemented a base class).&amp;#160; Sadly even when I removed the base class I was still getting the results.&lt;/p&gt;  &lt;p&gt;After a bunch of digging I was able to find the root cause, Derik was doing something stupid and jQuery was making it worse :)&lt;/p&gt;  &lt;p&gt;In order to get some feedback and to help illustrate my issue.&lt;/p&gt;  &lt;p&gt;Here is what my Typescript class which was having the issue looked like.&amp;#160; The box in red is what was throwing the error.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_7340CAAC.png"&gt;&lt;img title="image" style="border-left-width:0px;border-right-width:0px;border-bottom-width:0px;display:inline;border-top-width:0px;" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_061A6FB9.png" width="546" height="484" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;From Chrome’s debugger when I would output ‘this’ it would be the browsers window object, not my parents object.&amp;#160; This told me something was screwed up but I could not find the cause.&amp;#160; That was until I started walking the call stack to see WHERE PerformSearch was being called.&amp;#160; Turns out PerformSearch was being called by jQuery… that was odd.&amp;#160; In fact below is the code making the call.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_0781233C.png"&gt;&lt;img title="image" style="border-left-width:0px;border-right-width:0px;border-bottom-width:0px;display:inline;border-top-width:0px;" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_373ECBBB.png" width="653" height="159" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;Again, why was jQuery calling my method I had not told it to.&amp;#160; Well I kept walking the stack and I realized that this was being called during a .ajax call as seen below.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_2BA90E7C.png"&gt;&lt;img title="image" style="border-left-width:0px;border-right-width:0px;border-bottom-width:0px;display:inline;border-top-width:0px;" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_2013513D.png" width="555" height="58" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It turns out that the ‘data’ object I was passing across the wire was not just a property bag, but it had methods.&amp;#160; These methods referenced ‘this’ and when jQuery tried to searlize the object it is executing the functions, and when it does the ‘this’ context is wrong and BOOM.&lt;/p&gt;  &lt;p&gt;To solve this issue I simply refactored my code to NOT have a method and then it just worked.&lt;/p&gt;  &lt;p&gt;My question is this.&amp;#160; Is what I was doing just dumb and I was an idiot, or is there a way to tell jQuery to NOT try and call my methods during serialization?&amp;#160; &lt;/p&gt;  &lt;p&gt;Till Next time,&lt;/p&gt;  &lt;p&gt;P.S. I know I could have guarded against this in my method by checking that ‘this != window’ but that felt dirty and I had a better solution.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=109644" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Typescript/default.aspx">Typescript</category></item><item><title>Help I am getting an XMLHttpRequest exception of Access-Control-Allow-Orig</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2013/02/10/help-i-am-getting-an-xmlhttprequest-exception-of-access-control-allow-orig.aspx</link><pubDate>Sun, 10 Feb 2013 12:34:06 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:80352</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=80352</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=80352</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2013/02/10/help-i-am-getting-an-xmlhttprequest-exception-of-access-control-allow-orig.aspx#comments</comments><description>&lt;p&gt;When working on a demo application recently I was trying to get an ajax call to hit one of my Web API routes and I was immediately hit with the following exception.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;XMLHttpRequest cannot load &lt;a href="http://localhost:33884/api/ToDo/"&gt;http://localhost:33884/api/ToDo/&lt;/a&gt;. Origin &lt;a href="http://localhost/"&gt;http://localhost:3462&lt;/a&gt; is not allowed by Access-Control-Allow-Orig&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Of course this sucked because I KNEW what the issue was but not exactly how to solve it.&amp;#160; Well solve it the way I wanted.&amp;#160; Of course i could have just put both my sites (MVC project and WebAPI project) under the same web application in IIS to remove this but that was not an acceptable answer for my needs.&lt;/p&gt;  &lt;p&gt;After a bit of searching online I thought I came across an solution that would work.&amp;#160; I thought that I could just set the &lt;strong&gt;&lt;a href="http://api.jquery.com/jQuery.ajax/"&gt;crossDomain&lt;/a&gt;&lt;/strong&gt; switch in my ajax call as below to get it to work, but no dice.&lt;/p&gt;  &lt;pre class="c#" style="display:none;" name="code"&gt;$.ajax({
    url: url,
    type: &amp;#39;Get&amp;#39;,
    crossDomain: true,
    success: (data) = &amp;amp; gt; {
        // do something here
    },
    error: (data) = &amp;amp; gt; { }
});&lt;/pre&gt;

&lt;p&gt;I found another suggestion telling me to change the dataType of request form json to jsonp as below, but as you may have guessed that did not work.&lt;/p&gt;

&lt;pre class="c#" style="display:none;" name="code"&gt;$.ajax({
    url: url,
    type: &amp;#39;Get&amp;#39;,
    dataType: jsonp,
    success: (data) = &amp;amp; gt; {
        // do something here
    },
    error: (data) = &amp;amp; gt; { }
});&lt;/pre&gt;

&lt;p&gt;Finally at a bit more searching I found more information which suggested I needed to add a response header on the server side to allow ‘Access-Control-Allow-Origin’ and this &lt;a href="http://stackoverflow.com/questions/14104289/strange-behavior-when-using-jquery-asp-net-webapi/14105313#14105313"&gt;Stack Overflow&lt;/a&gt; answer gave me all the details needed to make this happen in Web API.&amp;#160; Once I implemented the solution inside my WebApi endpoints and REMOVED the 2 un-needed attributes in my .ajax call the world was a happy place and I was able to make cross domain calls to my other services.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=80352" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/ASP.Net+MVC/default.aspx">ASP.Net MVC</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WebApi/default.aspx">WebApi</category></item><item><title>Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2012/12/20/asp-net-web-api-mediatypeformatter-error-for-x-www-formurlencoded-data.aspx</link><pubDate>Thu, 20 Dec 2012 13:06:17 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:70727</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=70727</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=70727</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2012/12/20/asp-net-web-api-mediatypeformatter-error-for-x-www-formurlencoded-data.aspx#comments</comments><description>&lt;p&gt;As I was breaking out some of my MVC controllers to use Web Api I ran into a not so fun error (seen below) when doing a jQuery Post.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_60A45A9F.png"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_6FF7BCAE.png" width="680" height="50" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;I was trying to post a model via jQuery to my Web Api Endpoint and it was failing and it made no sense because when I used Postman via Chrome it would work just fine.&amp;#160; My jQuery code is below&lt;/p&gt;  &lt;pre class="c#" name="code"&gt;var model = {
    GroupId: self.SelectedGroup().GroupId,
    Name: self.SelectedGroup().Name,
};

$.ajax({
    url: route,
    type: &amp;#39;Post&amp;#39;,
    dataType: &amp;quot;json&amp;quot;,
    data: model,
    success: function(data){ 
        alert(&amp;quot;passed&amp;quot;);
    },
    error: function(data) {
        alert(&amp;#39;There was a (&amp;#39; + data.status + &amp;#39;) returned due to &amp;#39; + data.statusText + &amp;#39;.&amp;#39;); //or whatever
    }
});&lt;/pre&gt;

&lt;p&gt;My Postman setup looked like&lt;/p&gt;

&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_16599FFA.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_23BFB300.png" width="607" height="353" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Turns out to find my issue i needed to inspect the error result passed back to jQuery which pointed me towards the real error of&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&amp;quot;No MediaTypeFormatter is available to read an object of type &amp;#39;GroupSelectionModel&amp;#39; from content with media type &amp;#39;application/x-www-form-urlencoded&amp;#39;.&amp;quot;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that I knew that jQuery was trying to do a FormUrlEncoded post, not a pure json post I had an idea what the root cause was… ME.&amp;#160; I was trying to be clever and only allow for the JsonMediaTypeFormatter and removing ALL other formatters, see below.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_0338734E.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_02CC4059.png" width="469" height="63" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;What I needed to do was the following&lt;/p&gt;

&lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_6D6E8AF0.png"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_05FE2841.png" width="528" height="75" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Once I added the correct formatter it all just WORKED.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=70727" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/WebApi/default.aspx">WebApi</category></item><item><title>Adding JQuery UI to MVC 3 Application causes a JScript Runtime exception</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/10/adding-jquery-ui-to-mvc-3-application-causes-a-jscript-runtime-exception.aspx</link><pubDate>Sat, 10 Sep 2011 11:08:27 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:68178</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=68178</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=68178</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/09/10/adding-jquery-ui-to-mvc-3-application-causes-a-jscript-runtime-exception.aspx#comments</comments><description>&lt;p&gt;Today I added a reference to &lt;a href="http://jqueryui.com/"&gt;jQuery&lt;/a&gt; UI via &lt;a href="http://nuget.org"&gt;Nuget&lt;/a&gt; to the rewrite of the &lt;a href="http://www.dimecasts.net"&gt;Dimecasts.net&lt;/a&gt; website and as soon as I launched the site locally I was presented with the following dialog:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_0497C27E.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_7CA0201B.png" width="404" height="184" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Now given the fact that the ONLY change to my code base was adding the scripts to my scripts folder as well as adding the 2 lines to my _Layout.cshtml file which includes the jQuery in the application as below I knew the error had to be w/ the adding of the script tags.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_709E2FE7.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_2FFBE378.png" width="644" height="102" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In order to prove my theory I simply removed the script tag which included jquery-ui-1.8.16 and restarted the site.&amp;#160; Sure enough everything worked just fine.&amp;#160; When I took a step back and better studied the error it made sense to me that jquery-ui MUST be after the jQuery script tag. So I moved the script tag to the end of the list as below and restarted the site.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_728B7EF0.png"&gt;&lt;img style="background-image:none;border-bottom:0px;border-left:0px;padding-left:0px;padding-right:0px;display:inline;border-top:0px;border-right:0px;padding-top:0px;" title="image" border="0" alt="image" src="http://devlicio.us/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/derik_5F00_whittaker/image_5F00_thumb_5F00_18ED623C.png" width="644" height="89" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Sure enough all is right with the world and I no longer have any runtime JScript engine exceptions.&amp;#160; Now it is time to make use of the JQuery UI stuff.&lt;/p&gt;  &lt;p&gt;Hope this helps someone in the future.&lt;/p&gt;  &lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=68178" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/ASP.Net+MVC/default.aspx">ASP.Net MVC</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/DimeCasts.Net/default.aspx">DimeCasts.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/jQuery/default.aspx">jQuery</category></item></channel></rss>