🌐 AI搜索 & 代理 主页
Skip to content

Commit 90a87c0

Browse files
committed
Switched to using new Function instead of eval for handling JSON parsing (Fixes bug #4680). Added support for JSON.parse, if it exists (Fixes bug #4429).
1 parent a0451f1 commit 90a87c0

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/ajax.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -481,24 +481,32 @@ jQuery.extend({
481481
xml = type == "xml" || !type && ct && ct.indexOf("xml") >= 0,
482482
data = xml ? xhr.responseXML : xhr.responseText;
483483

484-
if ( xml && data.documentElement.tagName == "parsererror" )
484+
if ( xml && data.documentElement.tagName == "parsererror" ) {
485485
throw "parsererror";
486+
}
486487

487488
// Allow a pre-filtering function to sanitize the response
488489
// s != null is checked to keep backwards compatibility
489-
if( s && s.dataFilter )
490+
if ( s && s.dataFilter ) {
490491
data = s.dataFilter( data, type );
492+
}
491493

492494
// The filter can actually parse the response
493-
if( typeof data === "string" ){
495+
if ( typeof data === "string" ) {
494496

495497
// If the type is "script", eval it in global context
496-
if ( type == "script" )
498+
if ( type === "script" ) {
497499
jQuery.globalEval( data );
500+
}
498501

499502
// Get the JavaScript object, if JSON is used.
500-
if ( type == "json" )
501-
data = window["eval"]("(" + data + ")");
503+
if ( type == "json" ) {
504+
if ( typeof JSON === "object" && JSON.parse ) {
505+
data = JSON.parse( data );
506+
} else {
507+
data = (new Function("return " + data))();
508+
}
509+
}
502510
}
503511

504512
return data;

test/unit/ajax.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,25 @@ test("jQuery.getJSON(String, Function) - JSON object", function() {
693693
});
694694
});
695695

696+
test("jQuery.getJSON - Using Native JSON", function() {
697+
expect(2);
698+
699+
var old = window.JSON;
700+
JSON = {
701+
parse: function(str){
702+
ok( true, "Verifying that parse method was run" );
703+
return true;
704+
}
705+
};
706+
707+
stop();
708+
jQuery.getJSON(url("data/json.php"), function(json) {
709+
window.JSON = old;
710+
equals( json, true, "Verifying return value" );
711+
start();
712+
});
713+
});
714+
696715
test("jQuery.getJSON(String, Function) - JSON object with absolute url to local content", function() {
697716
expect(2);
698717

0 commit comments

Comments
 (0)