Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Created August 2, 2014 00:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffjohnson9046/458a0c22192f1ce9eaf3 to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/458a0c22192f1ce9eaf3 to your computer and use it in GitHub Desktop.
jQuery JSONP Request
// This is something I ALWAYS forget about, so hopefully this Gist will serve as a reminder. Whenever you get the "Access-Control-Allow-Origin"
// error, that's because you're trying to make a cross-domain AJAX request, which is a big no-no in the world of the internet.
// So, after spending WAY more time than I care to admit trying to address this, I remembered that JSONP is the answer. Here's
// how to do it:
$.ajax({
url: [/* whatever your URL is */],
/*type: 'GET',*/ // seems superfluous - .ajax uses GET by default.
dataType: 'jsonp',
/*crossDomain: true,*/ // seems to work fine without this.
data: { /* an object with whatever data you need to pass along in your JSONP request */ },
success: function(response) {
/* do something meaningful with the result. */
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment