Skip to content

Commit

Permalink
Wrap the JSON parsing calls with a try/catch expression to catch Synt…
Browse files Browse the repository at this point in the history
…axErrors various libraries throw to signal malformed json.
  • Loading branch information
dantman committed Feb 17, 2009
1 parent 2cef3eb commit f8e62c0
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/ajax.js
Expand Up @@ -539,11 +539,16 @@ jQuery.extend({
},

json: function( data ) {
var func = ( window.JSON && window.JSON.parse ) // Native JSON and json2.js
|| window.json_parse // json_parse.js and json_parse_state.js
|| window.jsonParse; // json_sans_eval.js
try {
var func = ( window.JSON && window.JSON.parse ) // Native JSON and json2.js
|| window.json_parse // json_parse.js and json_parse_state.js
|| window.jsonParse; // json_sans_eval.js

return func ? func( data ) : window["eval"]("(" + data + ")");
return func ? func( data ) : window["eval"]("(" + data + ")");
} catch (err) {
if(err.name != "SyntaxError") throw err;
return false;
}
}

});

0 comments on commit f8e62c0

Please sign in to comment.