Created
March 4, 2013 10:00
-
-
Save vincicat/5081199 to your computer and use it in GitHub Desktop.
How to kill your jQuery AJAX call with an HTML valid url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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!")}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: You can use
@data-url
in any html element to replace the code above.