Ajax调用PHP脚本返回404错误 [英] Ajax call to php script returns 404 error

查看:100
本文介绍了Ajax调用PHP脚本返回404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个字preSS设计师,我开发了一个接触的形式为我的主题,通过jQuery的验证之一。

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

请检查code以下,然后阅读下面的注意事项。

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
            }

        });
    }
});

注:

  • sendEmail.php是,使用的PHPMailer类发送电子邮件,一个正确的脚本。
  • 的templatePath变量的完整模板路径看起来像这样的值:的http:// somedomain .COM /可湿性粉剂内容/主题/ THEMENAME
  • 在jQuery的code以上位于LIB /脚本/ jfunctions.js(PHP脚本的同一目录下)
  • 在整个过程(Ajax和PHP)完美的作品如预期的多台服务器,(我的主题用户在两台服务器由我和其他服务器进行测试)。
  • 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).

的问题:

在某些服务器的成功处理程序就不会被触发,而Ajax调用sendEmail.php实际上是通过成功的PHP脚本被处理和发送电子邮件。

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.

当我检查与萤火虫明白为什么成功处理程序不会被触发,萤火显示未找到404错误,这就像一场虚惊。

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.

可能的原因:

我觉得有些服务器配置为阻止这样的Ajax调用。

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

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

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

在此先感谢。

@nowk:sendEmail.php code是:

@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>';
    }
}
?>

请注意,上面的code完全执行,即使AJAX返回404,奇怪吧!

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

推荐答案

由于服务器发送404(因为神知道什么原因),有两种方法可以解决/规避这样的:

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

  1. 忽略的HTTP响应code和修改成功完整在jQuery的Ajax调用,从而使在处理器执行时,请求完成不管服务器响应。你知道服务器响应(它始终工作)。 HTML的应该还是在jQuery的可用完整处理程序。
  2. 头(HTTP / 1.1 200:
  3. 在覆盖执行的东西发送服务器(可能是一些字preSS)的404(打印任何输出之前的) OK')。由于执行脚本,这将覆盖疯狂的404和jQuery将收到200执行成功处理程序。
  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.

您可以尝试两种=)我是pretty的肯定第一个会工作(但是这不是那么干净)。我也pretty的肯定第二届会的工作,但我不知道字preSS不够好,使承诺=)

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

这篇关于Ajax调用PHP脚本返回404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆