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

Commit 44e6beb

Browse files
committed
Make sure we do the malformed JSON check for all both JSON.parse and new Function (this helps to create uniformity between browser implementations of JSON.parse - like where Chrome allows some malformed strings. Thanks to DBJDBJ for the heads-up.
1 parent 23d600c commit 44e6beb

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/ajax.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -572,19 +572,22 @@ jQuery.extend({
572572
if ( typeof data === "string" ) {
573573
// Get the JavaScript object, if JSON is used.
574574
if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {
575-
// Try to use the native JSON parser first
576-
if ( window.JSON && window.JSON.parse ) {
577-
data = window.JSON.parse( data );
578-
579575
// Make sure the incoming data is actual JSON
580576
// Logic borrowed from http://json.org/json2.js
581-
} else if (/^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
577+
if (/^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
582578
.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
583579
.replace(/(?:^|:|,)(?:\s*\[)+/g, ""))) {
580+
581+
// Try to use the native JSON parser first
582+
if ( window.JSON && window.JSON.parse ) {
583+
data = window.JSON.parse( data );
584+
585+
} else {
584586
data = (new Function("return " + data))();
587+
}
585588

586589
} else {
587-
throw "JSON.parse";
590+
throw "Invalid JSON: " + data;
588591
}
589592

590593
// If the type is "script", eval it in global context

0 commit comments

Comments
 (0)