YouTube Uploads Using CORS

What's CORS?

Normally, when you use XMLHttpRequest to make HTTP requests from JavaScript, you can only contact the same server that your HTML page is served off of.

This is very limiting as it prevents you from talking directly to a third-party API without having to use a local proxy service or a technique like JSONP that only works for GETs.

If an API's server has been enabled for CORS, and you're using a supported web browser, then your JavaScript code can make XMLHttpRequests to that API regardless of the domain your web page is served from.

This is usually cleaner than JSONP, and it means that you can do things like set custom request headers and use methods other than GET.

Great! Let's see an example.

The following form will allow you to use OAuth 2 to grant access to your YouTube account, and then upload a new video to YouTube.

One of the steps involved in uploading the video is sending a HTTP POST to the YouTube API, and before CORS support, this needed to be done server-side.

It's now possible to do all this client-side!

Upload A New Video File

You're logged in as