Sometimes, data returned from the server is not defined which results in the following appearing on my page:
<p id="element">undefined</p>
My AJAX success function:
success: function( data ) {
$( '#element' ).text( data.message )
}
PHP handler:
$response = array()
// Some logic which may assign data to $response['message']
echo json_encode( $response );
die;
Is there a way to suppress such undefined messages?
Depending on what you want, you can show message only if it was provided
if (data.message) {
$('#element').text(data.message)
}
or maybe show default message if server returns nothing (or empty string):
$('#element').text(data.message || 'No message.')
success: function( data ) {
if( data.message !== undefined )
$( '#element' ).text( data.message )
}