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

我是一名WordPress设计师,我为我的一个主题开发了一个联系表单,通过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.
  • sendEmail.php是一个使用PHPmailer类发送电子邮件的正确脚本。
  • templatePath variable has the value of the full template path which looks like this: http://somedomain.com/wp-content/themes/themename
  • templatePath变量具有完整模板路径的值,如下所示:http://somedomain.com/wp-content/themes/themename
  • The jQuery code above is located in lib/scripts/jfunctions.js (same directory of the php script)
  • 上面的jQuery代码位于lib / scripts / jfunctions.js(php脚本的相同目录)
  • 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).
  • 整个过程(ajax和php)在许多服务器中完美地工作,(由我和其他服务器在我的主题用户的两台服务器中测试)。

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.

在某些服务器中,当成功传递对sendEmail.php的ajax调用并处理php脚本并发送电子邮件时,不会触发成功处理程序。

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.

当我用firebug检查为什么没有触发成功处理程序时,firebug显示“找不到404错误”,这就像是一个误报。

Possible causes:

可能的原因:

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

我认为有些服务器被配置为阻止这样的ajax调用。

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

这个奇怪的问题可能是什么原因?怎么解决?

Thanks in advance.

提前致谢。

@nowk: sendEmail.php code is:

@nowk:sendEmail.php代码是:

<?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!.

请注意,即使ajax返回404,上面的代码也能完美执行,很奇怪吧!

4 个解决方案

#1


26

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

由于服务器发送404(上帝知道是什么原因),有两种方法可以解决/规避这个问题:

  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. 忽略HTTP响应代码并在jQuery ajax调用中更改成功以完成,以便在请求完成时执行处理程序,无论服务器响应如何。您知道服务器响应(它始终有效)。 HTML应该仍然可以在jQuery完整处理程序中使用。
  3. 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.
  4. 通过执行(在打印任何输出之前)覆盖事物在服务器上发送的404(可能是Wordpress):header('HTTP / 1.1 200 OK')。由于脚本被执行,这将覆盖疯狂的404,jQuery将接收200并执行成功处理程序。

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 =)

你可以试试两个=)我很确定第一个会起作用(但那不是那么干净)。我也很确定第二个会工作,但我不知道Wordpress是否足以做出承诺=)

更多相关文章

  1. 使用jquery的$ .ajax来获取node.js脚本的输出,而不是它的代码
  2. Chrome扩展程序开发:使用jQuery修改DOM
  3. Server.Htmlencode用于文本域显示带的html代码
  4. IOS学习之WebView加载本地HTML代码或网络资源
  5. html5 css3 背景视频循环播放代码
  6. 小程序:Java下载单页HTML(可下载引用资源)
  7. HTML5 标签audio添加网页背景音乐代码
  8. Html5 Canvas+Javascript实现一个简单画图程序(三)
  9. Web 应用程序学习笔记

随机推荐

  1. Android(安卓)如何建立AIDL
  2. 包装模式就是这么简单啦
  3. Intent妙用之编写自己的android主界面
  4. 还在使用if-else?试一下策略模式
  5. 理解Git Submodules
  6. PreferenceActivity用法简介【转】
  7. 单例模式的十种写法,你会几个?(修补了几个错
  8. Lottie动画简介
  9. ym——Android从零开始(5)(文件操作+日记
  10. Android轻松集成Camera拍照