Skip to content

Commit 308d6cd

Browse files
committed
Make sure that a parsererror is thrown whenever malformed JSON comes back from a server (so that the Ajax error handler is called). Makes it uniform across browsers that do and don't have JSON.parse support.
1 parent c14fa51 commit 308d6cd

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/ajax.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,20 +570,26 @@ jQuery.extend({
570570

571571
// The filter can actually parse the response
572572
if ( typeof data === "string" ) {
573-
// If the type is "script", eval it in global context
574-
if ( type === "script" || !type && ct.indexOf("javascript") >= 0 ) {
575-
jQuery.globalEval( data );
576-
}
577-
578573
// Get the JavaScript object, if JSON is used.
579574
if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
580575
// Try to use the native JSON parser first
581576
if ( window.JSON && window.JSON.parse ) {
582577
data = window.JSON.parse( data );
583578

579+
// Make sure the incoming data is actual JSON
580+
// Logic borrowed from http://json.org/json2.js
581+
} else if (/^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
582+
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
583+
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
584+
data = (new Function("return " + data))();
585+
584586
} else {
585-
data = (new Function("return " + data))();
587+
throw "JSON Syntax Error: " + data;
586588
}
589+
590+
// If the type is "script", eval it in global context
591+
} else if ( type === "script" || !type && ct.indexOf("javascript") >= 0 ) {
592+
jQuery.globalEval( data );
587593
}
588594
}
589595

test/data/badjson.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{bad: 1}

test/unit/ajax.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,13 @@ test("jQuery.param()", function() {
341341

342342
test("synchronous request", function() {
343343
expect(1);
344-
ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), async: false}).responseText ), "check returned text" );
344+
ok( /^{ "data"/.test( jQuery.ajax({url: url("data/json_obj.js"), dataType: "text", async: false}).responseText ), "check returned text" );
345345
});
346346

347347
test("synchronous request with callbacks", function() {
348348
expect(2);
349349
var result;
350-
jQuery.ajax({url: url("data/json_obj.js"), async: false, success: function(data) { ok(true, "sucess callback executed"); result = data; } });
350+
jQuery.ajax({url: url("data/json_obj.js"), async: false, dataType: "text", success: function(data) { ok(true, "sucess callback executed"); result = data; } });
351351
ok( /^{ "data"/.test( result ), "check returned text" );
352352
});
353353

@@ -821,6 +821,25 @@ test("jQuery.ajax() - script, Remote with scheme-less URL", function() {
821821
});
822822
});
823823

824+
test("jQuery.ajax() - malformed JSON", function() {
825+
expect(1);
826+
827+
stop();
828+
829+
jQuery.ajax({
830+
url: "data/badjson.js",
831+
dataType: "json",
832+
success: function(){
833+
ok( false, "Success." );
834+
start();
835+
},
836+
error: function(xhr, msg) {
837+
equals( "parsererror", msg, "A parse error occurred." );
838+
start();
839+
}
840+
});
841+
});
842+
824843
test("jQuery.ajax() - script by content-type", function() {
825844
expect(1);
826845

0 commit comments

Comments
 (0)