Amin Darvand
Amin Darvand

Reputation: 367

How to See Errors in ajax.php

I call ajax.php through an AJAX request and try to parse the JSON when it responds. I have a mail() function in ajax.php which causes error. I need to see what error it is. It's really weird.. I've been struggling with it for hours.

When I access ajax.php through browser, the mail() works and I am able to receive the email. But when I call via AJAX, and read the response, it gives parserror and it's not good enough to find the cause. Here's the simplified code:

ajax.php:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
error_log("test");

goto mail;

//skipped code

mail:
try {

    $message = 'Test Mail';
    $from = "[email protected]";
    $to = "[email protected]";
    $subject = "Application";
    $headers = "From:" . $from;
    mail($to, $subject, $message, $headers);
} catch (Exception $e) {
    $errors[] = $e->getMessage();
}

if(count($errors)>0){
    echo json_encode(array('status'=>'error','message'=>implode($errors,'<br>')));
}
die();

global.js:

$('#theForm').on('submit',function(e){
    e.preventDefault();

    let formData = new FormData();

    formData.append('fname',$('#theForm input[name=first_name]').val());
    //... appending lots of data + file

    $.ajax({
        contentType: false,
        processData: false,
        cache: false,
        type: 'POST',
        dataType: 'json',
        url: '/lib/ajax.php',
        data: formData,
        success: function (data) {
            //celebrate
        },
        error: function (XMLHttpRequest, textStatus, errorThrown){
            $('.submit-result').html(textStatus);
        },
        timeout: 5000
    });
});

PS. I wish I could have error_log so I could see the PHP errors. It's a subdomain on a shared hosting, so I can't edit php.ini. But it does create error_log on the main domain.

Upvotes: 0

Views: 1059

Answers (1)

AlexioVay
AlexioVay

Reputation: 4527

Check the Network with your browser inspect tools. I especially recommend the Chrome browser by Google for that.

More insights: https://developers.google.com/web/tools/chrome-devtools/network

Upvotes: 1

Related Questions