Skip to content

Instantly share code, notes, and snippets.

@mohamedmansour
Created March 29, 2015 01:04
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 mohamedmansour/33802d10fcf218e28fc6 to your computer and use it in GitHub Desktop.
Save mohamedmansour/33802d10fcf218e28fc6 to your computer and use it in GitHub Desktop.
JavaScript Decruft JSON
// In jQuery the easy thing to do when to decruft json responses if a javascript has:
//
// for (;;); {foo: 'bar'}
//
// Is to decruft it using ajax converters. Example
$.ajaxSetup({
converters: {
"text json": function(result) {
return JSON.parse(result.replace('for (;;);', ''));
}
}
});
$.ajax(url, {
data: data,
contentType: 'application/x-www-form-urlencoded',
type: 'POST',
dataType:"text json"
}).done(function(resp) {
callback(resp);
}).fail(function(jqXHR, textStatus ) {
errorCallback(textStatus);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment