Skip to content

Commit 94d925c

Browse files
committed
Make sure leading whitespace is trimmed for parseJSON. Fixes #6031.
1 parent 8b86004 commit 94d925c

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/core.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,9 @@ jQuery.extend({
480480
if ( typeof data !== "string" || !data ) {
481481
return null;
482482
}
483+
484+
// Make sure leading/trailing whitespace is removed (IE can't handle it)
485+
data = jQuery.trim( data );
483486

484487
// Make sure the incoming data is actual JSON
485488
// Logic borrowed from http://json.org/json2.js

test/unit/core.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,16 @@ test("jQuery.proxy", function(){
807807
});
808808

809809
test("jQuery.parseJSON", function(){
810-
expect(7);
810+
expect(8);
811811

812812
equals( jQuery.parseJSON(), null, "Nothing in, null out." );
813813
equals( jQuery.parseJSON( null ), null, "Nothing in, null out." );
814814
equals( jQuery.parseJSON( "" ), null, "Nothing in, null out." );
815815

816816
same( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
817817
same( jQuery.parseJSON('{"test":1}'), {"test":1}, "Plain object parsing." );
818+
819+
same( jQuery.parseJSON('\n{"test":1}'), {"test":1}, "Make sure leading whitespaces are handled." );
818820

819821
try {
820822
jQuery.parseJSON("{a:1}");
@@ -829,4 +831,4 @@ test("jQuery.parseJSON", function(){
829831
} catch( e ) {
830832
ok( true, "Test malformed JSON string." );
831833
}
832-
});
834+
});

0 commit comments

Comments
 (0)