2

I have looked through previous answers but i cant find anything.

I am using this to send my response;

echo json_encode($response);

From inspecting the result I am receiving this to the page in the Response;

    <br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mail(): Failed to connect to mailserver at &amp;quot;localhost&amp;quot; port 25, verify your &amp;quot;SMTP&amp;quot; and &amp;quot;smtp_port&amp;quot; setting in php.ini or use ini_set() in C:\wamp64\www\ci7\php\MailSender.php on line <i>68</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0018</td><td bgcolor='#eeeeec' align='right'>377472</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\ci7\php\contact.php' bgcolor='#eeeeec'>...\contact.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0039</td><td bgcolor='#eeeeec' align='right'>400616</td><td bgcolor='#eeeeec'>Apolo\MailSender->send(  )</td><td title='C:\wamp64\www\ci7\php\contact.php' bgcolor='#eeeeec'>...\contact.php<b>:</b>58</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>3</td><td bgcolor='#eeeeec' align='center'>0.0040</td><td bgcolor='#eeeeec' align='right'>401224</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.mail' target='_new'>mail</a>
(  )</td><td title='C:\wamp64\www\ci7\php\MailSender.php' bgcolor='#eeeeec'>...\MailSender.php<b>:</b>68</td></tr>
</table></font>
{"status":"fail","errors":"Could not send a mail, sorry. Please try again."}

However, I get the error and i am not sure why?

This is the javascript;

        $.ajax({
        url: config.url,
        type: 'POST',
        dataType: 'json',
        data: $form.serialize(),
        success: function(data){
            if(data.status && data.status == 'fail') {
                $.Apolo.modules.alertMessage({
                    target: $form,
                    type: 'error',
                    message: data.errors,
                    icon: 'warning'
                });
                $form.trigger('apolo.contactFormMessage');
                config.onError.call($form, data);
            }
            else if(data.status && data.status == 'success') {
                $.Apolo.modules.alertMessage({
                    target: $form,
                    type: 'success',
                    message: data.statusText,
                    icon: 'check'
                });
                $form.find('input, textarea').val('');
                $form.trigger('apolo.contactFormMessage');
                config.onSuccess.call($form, data);
            }
        },
        error: function(jqXHR, textStatus, errorThrown){
            $.Apolo.modules.alertMessage({
                target: $form,
                type: 'error',
                message: errorThrown,
                icon: 'warning'
            });
            $form.trigger('apolo.contactFormMessage');
            config.onError.call($form, arguments);
        }
    });

3 Answers 3

1

Here's what's happening:

https://www.kevinleary.net/syntax-error-unexpected-token-json-position-0/

If you’re seeing a SyntaxError: Unexpected token < in JSON at position 0 error in your browser console at angular.js:13920, or Line 13920 or angular.min.js, then it’s likely that you’re working with HTTP API’s, possibly using $resource or $http, and one of the API’s has an error notice or warning in the response body.

In other words, the "response" you're getting is NOT JSON. It's HTML - an HTML error message. With a JSON string at the end.

You're doing the right thing - you have both success: function(data) and error: function() callbacks in your XHR request.

You need to:

  1. Ensure that an "error" response from the server results in triggering the "error" callback in your Javascript, and/or

  2. Detect when the server is sending a non-JSON response to either callback.

Sign up to request clarification or add additional context in comments.

1 Comment

As per the above, the mail function was generating a PHP error which was being returned in HTML before the JSON response. Suppressing the mail function error with '@' stopped the error prior to the response I required.
1

Ok I have solved the issue by adding @ in the mail function which removes the x-debug error and gives a clean response i.e.

            if(@!mail($email, $subject, $message, $headers)){

            $this->addError('Could not send a mail, sorry. Please try again.');
            return false;

        }

2 Comments

In other words, you've modifed your PHP back-end to suppress the PHP-generated HTML error text your Javascript front-end was gagging on: php.net/manual/en/language.operators.errorcontrol.php that
Exactly and as no PHP errors are returned, then you only receive the JSON response.
0

Try parse object response:

var objData = JSON.parse(data);

or in Ajax

dataType: 'application/json'

or in server

header('Content-Type: application/json');
echo json_encode($response);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.