a little JSON content validation in globalEval

a little JSON content validation in globalEval

The SHOUTcast 2.0 API has an issue where sometimes requests for JSON come back as XML. By adding a couple lines of code to jQuery 1.5.1 I was able to properly deal with this by throwing an error instead of having a runtime error.

I thought this may be useful for someone experiencing similar weirdness.

Line 54 in jquery-1.5.1.js:
  1. // JSON RegExp
  2. rvalidfirstchar = /^(\[|{)+/,
  3. ...

Around line 588 in jquery-1.5.1.js:
  1. // Evaluates a script in a global context
  2. globalEval: function( data ) {

  3.   if ( data && rnotwhite.test(data) ) {
  4.     // added by taber
  5.     if ( !rvalidfirstchar.test(data.substring(0, 1)) ) {
  6.       jQuery.error('Invalid JSON');
  7.       return;
  8.     }
  9.     //end added by taber

  10.     ...

  11.   }
  12. }