Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
994 views
in Technique[技术] by (71.8m points)

jquery - Ajax call to php script returns 404 error

I'm a WordPress designer, I developed a contact form for one of my themes that's validated via jQuery.

Please check the code below, then read the notes beneath.

$('.submitemail') .click(function() {

    //VALIDATION CODE GOES HERE

    if ( /*VALIDATED SUCCESSFULLY*/ ) {


        $.ajax({
            type: 'POST',
            url: templatePath+'/lib/scripts/sendEmail.php',
            data: 'visitorname=' + visitorname + '&visitoremail=' + visitoremail + '&visitormessage=' + visitormessage,

            success: function(contactResults) {
                //SUCCESS CODE
            }

        });
    }
});

Notes:

  • sendEmail.php is a correct script that sends email using PHPmailer class.
  • templatePath variable has the value of the full template path which looks like this: http://somedomain.com/wp-content/themes/themename
  • The jQuery code above is located in lib/scripts/jfunctions.js (same directory of the php script)
  • The whole process (ajax and php) works perfectly as expected in many servers, (tested in two servers by me and other servers by my theme users).

The Problem:

In SOME servers, the success handler is not triggered while the ajax call to sendEmail.php is actually passed successfully and the php script is processed and email is sent.

When I check with firebug to see why the success handler is not triggered, firebug shows "not found 404 error", It's like a false alarm.

Possible causes:

I think some servers is configured to block such ajax calls.

What might be the cause for this weird issue? How to fix it?

Thanks in advance.

@nowk: sendEmail.php code is:

<?php 
// Code for loading WordPress environment goes here //

$themeName_optionTree = get_option('option_tree');

$name = trim($_POST['visitorname']);
$email = $_POST['visitoremail'];
$message = $_POST['visitormessage'];


$site_owners_email = $themeName_optionTree['owner_email'];
$site_owners_name = $themeName_optionTree['owner_name'];
$email_subject = $themeName_optionTree['email_subject'];
$success_message = '<p class="success-box">' . $themeName_optionTree['success_message'] . '</p>';

if (strlen($name) < 2) {
    $error['name'] = 1; 
}

if (!preg_match('/^[a-z0-9&'.-_+]+@[a-z0-9-]+.([a-z0-9-]+.)*+[a-z]{2}/is', $email)) {
    $error['email'] = 1;    
}

if (strlen($message) < 2) {
    $error['message'] = 1;
}

if (!$error) {

    require_once('PHPMailer_v5.1/class.phpmailer.php');

    $mail = new PHPMailer(true);

    try {
        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = $email_subject;
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->Body = $message;
        $mail->Send();
        echo $success_message;
    } catch (phpmailerException $e) {
        echo '<p class="warning-box">' . $e->errorMessage() . '</p>';
    } catch (Exception $e) {
        echo '<p class="warning-box">' . $e->getMessage() . '</p>';
    }
}
?>

Please note that the above code executes perfectly even when ajax returns 404, weird huh!.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since the server sends a 404 (for god knows what reason), there are two ways to fix/circumvent this:

  1. Ignore the HTTP response code and change success to complete in the jQuery ajax call, so that the handler is executed when the request is done no matter the server response. You know the server response (it always works). The HTML should still be available in the jQuery complete handler.
  2. Overwrite the 404 that something sends on the server (probably something Wordpress) by executing (before printing any output): header('HTTP/1.1 200 OK'). Since the script is executed, this will overwrite the crazy 404 and jQuery will receive that 200 and execute the success handler.

You could try both =) I'm pretty sure the first one will work (but that's not so clean). I'm also pretty sure the 2nd will work, but I don't know Wordpress well enough to make promises =)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...