jQuery & Ajax - POST 406 error on form submit

Viewed 43

I see the POST 406 error in developer console when I try to press the submit button on a contact form:

Here is the PHP code from mail.php (am only posting the important part):

<?php
$toAddresses = array(
    'info@test.co.uk',
);
$subject = "Contact request by [username]";
$from = '[username]';
$fromAddress = "[useremail]";
$messages = array(
    'success'               => 'Thanks for your request.',
    'error_name_empty'      => 'Your Name is required.',
    'error_email_empty'     => 'Your E-Mail is required.',
    'error_message_empty'   => 'Message is required.',
    'error_mail_send'       => 'Sorry! System Error. Please us our E-Mail: <a href=\'mailto:'.$toAddresses[0].'\'>'.$toAddresses[0].'</a>',
);
$smtp = array(
    'activate'  => 0,
    'auth'      => 1,
    'host'      => 'mail3.gridhost.co.uk',
    'username'  => 'info@test.co.uk',
    'password'  => 'test',
    'port'      => 465
);
$return = array();
if( !isset( $return['errorMsg'] ) ) {
    require_once( $FilePhpMailer );
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $mail = new PHPMailer();
    $mail->IsHTML(true);
    $mail->CharSet  = 'UTF-8';
    $subject = str_replace( '[username]', $name, $subject );
    $subject = str_replace( '[useremail]', $email, $subject );
    $mail->Subject  = $subject;
    $from = str_replace( '[username]', $name, $from );
    $fromAddress = str_replace( '[useremail]', $email, $fromAddress );
    $mail->SetFrom( $fromAddress, $from );
    $mail->Body = include( $FileTemplateHtml );
    $mail->AltBody = include( $FileTemplateText );
    if( $smtp['activate'] == 1 ) {
        $mail->IsSMTP();
        $mail->Host = $smtp['host'];
        $mail->Port = $smtp['port'];
        if( $smtp['auth'] == 1 ) {
            $mail->SMTPAuth = TRUE;
            $mail->Username = $smtp['user'];
            $mail->Password = $smtp['password'];
        }
    }
    $one_email_success = 0;
    $email_send_error = 0;
    foreach( $toAddresses as $address ) {
        $mail->AddAddress( $address );
        if( $mail->Send() ) {
            $one_email_success = 1;
            $return['successMsg'] = $messages['success'];
        }
        $mail->ClearAddresses();
    }
    if( $one_email_success == 0 ) {
        $return['error'] = $messages['error_mail_send'];
    }
}
echo json_encode( $return );

here is the theme.mail.js code:

( function( $ ) {
    'use strict';
    $( '#contact-form' ).submit( function( event ) {
        $( '#contact-success' ).fadeOut();
        $( '#contact-error' ).fadeOut();
        event.preventDefault();
        var dataJson = $( this ).serialize();
        $.ajax( {
            type: 'POST',
            url: 'php-mail/mail.php',
            data: dataJson,
            dataType: 'json',
            success: function( contact ) {
                if( contact.successMsg ) {
                    $( '#contact-success > span' ).html( contact.successMsg );
                    $( '#contact-success' ).fadeIn();
                    $( '#contact-form' ).find( 'input[type=text], textarea' ).val( '' );
                } else if( contact.errorMsg ) {
                    $( '#contact-error > span' ).html( contact.errorMsg );
                    $( '#contact-error' ).fadeIn();
                }
            }
        } );
    } );
} ) ( jQuery );

Developer console screenshot of the POST 406 error:

enter image description here

Response:

<head><title>Not Acceptable!</title></head><body><h1>Not Acceptable!</h1><p>An appropriate representation of the requested resource could not be found on this server. This error was generated by Mod_Security.</p></body></html>

Network Panel screenshot:

enter image description here

Any idea on what could be causing this error?

0 Answers
Related