Server echoes warnings/errors that make a json expected response to be invalid
I'm currently writing an ajax form with jquery. The form is handled with a PHP script then the information is sent via the PHPMailer lite class. (http://phpmailer.worxware.com/index.php)
This is the PHP code:
if ($mail->Send())
echo '{"message":"Mensaje enviado con éxito","status":"ok"}';
else
echo '{"message":"Ocurió un error al envíar el mensaje, intentelo más tarde.","status":"fail"}';
I discovered this problem when the email submitted by the user is not a valid one, resultin开发者_如何学编程g in the following error:
Invalid address: E-mail:Invalid address: E-mail:Could not execute: /usr/sbin/sendmail
I should test the email. Ok. But the problem is that the previous error message "gets in the way" and corrupts the response. So I can solve the email error, but any other warning/error would corrupt the response.
A 'corrupted' response example:
Invalid address: E-mail:Invalid address: E-mail:Could not execute: /usr/sbin/sendmail {"message":"Ocurió un error al envíar el mensaje, intentelo más tarde.","status":"fail"}
(First line should not be there)
And this is the javascript (jquery) part:
$.ajax
({
type: "POST",
url: "contact.php",
data: $(this).serialize(),
success: function(data)
{
if(data.status == 'fail')
{
do something with data.message;
}
else if(data.status == 'ok')
{
do something else with data.message;
}
},
dataType: 'json'
});
Any help with this? :) thank you
Its a long shot but have you tried
@ if ($mail->Send())
The @ suppresses php errors on a given line of code.
精彩评论