Skip to content

Instantly share code, notes, and snippets.

@vincicat
Created March 4, 2013 10:00
Show Gist options
  • Save vincicat/5081199 to your computer and use it in GitHub Desktop.
Save vincicat/5081199 to your computer and use it in GitHub Desktop.
How to kill your jQuery AJAX call with an HTML valid url
// Situation: /echo need a `key` as a GET param
// - key: access token
// HTTP 200 - any `key`
// HTTP 403 - no `key`
var good_url = "/echo/?version=1&key=4bf3D"; //Good URL that will not break the server but it will fail the HTML validator
var html_url = "/echo/?version=1&key=4bf3D"; //HTML valid URL that will pass the html validator but fail $.get
$.get(good_url)
.done(function(){ console.log("OK"); }) //200, OK
.fail(function(){ console.log("Ka-boom!")});
$.get(html_url)
.done(function(){ console.log("OK"); })
.fail(function(){ console.log("Ka-boom!")}); //403, Ka-boom! (Tested in FF & Chrome)
// Reason: `key` is `amp;key` now
// Solution: unescape/decode as most HTML element does
// Source: http://stackoverflow.com/a/7656296
var decode_url = $("<a>").html(html_url).text();
$.get(decode_url)
.done(function(){ console.log("OK"); })
.fail(function(){ console.log("Ka-boom!")});
@vincicat
Copy link
Author

vincicat commented Mar 5, 2013

Note: You can use @data-url in any html element to replace the code above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment