A New Year’s Experiment – Writing on Medium

Medium-LogoThis year I’ve decided to write on new platform, Medium. The writing experience seems to be superior to WordPress so I’ve made it a goal to only write on there and see if my writing habits improve.

You can find all of my new posts there.

Advertisements

CORS, Internet Explorer 8, and XDomainRequest

Intro

I recently built a recipe app using a third-party API that supports CORS. Things were going along well, it was working well on all of the latest browsers (Chrome 28, Firefox 23, IE10). But when I started testing on Internet Explorer 8/9, I wasn’t getting any sort of response from the back-end. I immediately panicked, added a console.log fix and started placing breakpoints on my ajax statements to see what sort of request I was getting back.

The Problem

I came across a “No Transport” error being thrown by jQuery’s ajax. I had never heard of it, so after some searching on msdn and stackoverflow I came across a few suggestions to “quickly” fix this. Such as for jQuery 1.5+:

jQuery.support.cors = true;

After a couple other attempts, I accepted the fact that I will have to use IE’s XDomainRequest. To my astoundment, jQuery doesn’t natively support XDomainRequest.

Extending jQuery ajax to support XDomainRequest

Before I started implementing it, I came across a couple library’s on github and gists, but my journey to find the solution eventually lead me to @MoonScript’s jQuery-ajaxTransport-XDomainRequest repo on github. The examples provided worked like a charm on IE8, 9 and it looked like a reputable project, so I added a couple more tests to my outgoing ajax requests, then dropped in MoonScript’s library to my project. Problem solved. No need to reinvent the wheel here.

I couldn’t help myself so I’ll leave you with this:

Debug IE

Conclusion

Good news everybody, IE10 supports CORS using XMLHttpRequest. No more extending jQuery to support XDomainRequest.

XDomainRequest Gotchas

Some other things I came across that will save you a headache.

  1. The server protocol must be the same as the calling page protocol.
    1. This means you can’t make requests from file:// to http ://, http to https, or https to http (don’t ever do the latter)
    2. I found this the hard way by trying to run my code locally without first spinning up my localhost server.
  2. Only “text/plain” is supported for the request’s “Content Type header
  3. No custom headers can be added to the request

Resources:

Progress Brings Happiness

Getting things done makes the rest of the day even better. It gives leeway for the next day to be just as awesome. While I get plenty of of things done during the course of the day, I’m not talking about just knocking off an item from the todo list. I am talking about considerable progress [on a project]…seeing a feature work, committing it, pushing it to GitHub, then deploying it. Otherwise its sad panda.

Getting Things Done

Nexus 4 Launch

“Imagine that this had happened in a brick and mortar store: you wait in line for a new product, you get inside, you get the product, you go to the checkout, then the cash register breaks, so the manager takes the item that you were going to purchase and puts it back on the shelf, and tells you to try again. So, you fight through the crowd again to get the product, then as you’re walking to the checkout, the manager takes the item and puts it back on the shelf, but this time when you go back, the shelf is empty. That’s what happened to many people in the Play Store today.”