[#9655] Recover from malformed JSON checkout response

If a plugin or other code produces output during checkout processing,
it breaks the JSON parsing. This commit makes use of jQuery’s Ajax
dataFilter which allow us to sanitize the raw response before it’s
parsed by the Ajax handler.
This commit is contained in:
Max Rice 2015-12-02 17:31:43 -05:00
parent d585e65e5a
commit b50e830b54
2 changed files with 31 additions and 1 deletions

View File

@ -342,6 +342,36 @@ jQuery( function( $ ) {
}); });
} }
jQuery.ajaxSetup( {
dataFilter: function( raw_response ) {
try {
// check for valid JSON
var data = $.parseJSON( raw_response );
if ( data && typeof data === 'object' ) {
// return it so it can be parsed by Ajax handler
return raw_response;
}
} catch ( e ) {
// attempt to fix the malformed JSON
valid_json = raw_response.match( /{"result.*"}/ );
if ( null === valid_json ) {
console.log( 'Unable to fix malformed JSON' );
} else {
raw_response = valid_json[0];
}
}
return raw_response;
}
} );
$.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: wc_checkout_params.checkout_url, url: wc_checkout_params.checkout_url,

File diff suppressed because one or more lines are too long