为什么我会得到错误:“;500内部服务器错误”;在Wordpress上使用Ajax传递参数时


Why do I get the error: "500 Internal Server Error" when passing parameters using Ajax on Wordpress?

我的Wordspress网页上有一个动态联系表单。

这是HTML结构:

        <div class="form_content">
            <div> Name:    <span> <input type="text" id="fullname"> </span></div>
            <div> E-mail:  <span> <input type="text" id="email">    </span></div>
            <div> Message: <span> <input type="text" id="text">     </span></div>
            <div id="sendBtn"> Submit </div>
        </div>

我有一些函数可以从HTML中获取输入,并将它们发送到一个content.php文件中。

// Send button for the "contact form".
jQuery('#sendBtn').click(function(){
    //get info 
    var fullname = jQuery("#fullname").val();
    var email = jQuery("#email").val();
    var text = jQuery("#text").val();
    //send info to php 
    jQuery.ajax({
        beforeSend: function() {
            if ( IsEmail(email) == false) {
                jQuery('#aboutUnsuccess').show("slow");
                /*jQuery('#pixel-thing').show("fast");*/
                jQuery('.form_content').hide("slow");
            }
        },
        url: 'http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/contact.php', 
        type: "POST", 
        data: ({ "fullname": fullname, "email": email, "text": text }), 
        success: function (results){
            if ( IsEmail(email) == true) {
                //hide table 
                jQuery('.form_content').hide('slow', function() {
                    jQuery('.form_content').hide( "slow" );
                  });
                //show textboxes
                jQuery('#aboutSuccess').show("slow");
                jQuery( "#aboutSuccess" ).append( "<iframe id='"pixel-thing'" src='"http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/thePixel.html'" width='"1'" height='"1'"  border='"0'"></iframe>" );
            /************************************/
                window.google_trackConversion({
                  google_conversion_id: 666, 
                  google_remarketing_only: true
                });
            /*************************************/
            }
        }
    }); 

最后,我的content.php处理参数并将相关信息发送到我的电子邮件:

<?php 
require_once "Mail.php";
        if(isset($_POST['email'])) {
            $email = $_POST['email'];       
            $email_to = "info@example.com";
            $host = "ssl://smtp.gmail.com:465";
            $username = 'myEmail@email.com';
            $password = 'passpaspaspas';
            $email_subject = "You have a new email from $email via example.com website";
            $message = $_POST['text']; 
            $headers = array ('From' => $email, 'To' => $email_to,'Subject' => $email_subject);
            $smtp = Mail::factory('smtp',
              array ('host' => $host,
                'auth' => true,
                'username' => $username,
                'password' => $password));
            $mail = $smtp->send($email_to, $headers, $message);
            if (PEAR::isError($mail)) {
              echo($mail->getMessage());
            } else {
              echo("Message successfully sent!'n");
            }
        }

我正在把我的网站,建立没有任何MVC/CMS,到wordpress。这适用于我的"常规"网站。

为什么我的代码在wordpress中不起作用?

这是Chromes"网络"的输出:

Remote Address:54.159.xx.xx:80
Request URL:http://54.159.xx.xx/wp-content/themes/twentyfifteen-child/contact.php
Request Method:POST
Status Code:500 Internal Server Error
Request Headersview source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:63
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:wp-settings-1=editor%3Dtinymce%26posts_list_mode%3Dlist; wp-settings-time-1=1424359234
Host:54.149.xx.xx
Origin:http://54.149.xx.xx
Referer:http://54.149.xx.xx/?page_id=73
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
fullname: Test
email:Test@gmail.com
text:This is a test 
Response Headersview source
Connection:close
Content-Length:0
Content-Type:text/html
Date:Tue, 24 Feb 2015 14:13:46 GMT
Server:Apache/2.4.7 (Ubuntu)
X-Powered-By:PHP/5.5.9-1ubuntu4.5

Wordpress不喜欢直接发布到主题中的php文件。你应该做的是发布到/(或者你想发布到的任何模板/路径,比如"联系人"),并将表单处理代码放在你的主题文件中。

我建议将您的代码放在functions.php中,并添加一个操作来侦听ajax请求,有关详细描述,请参阅Wordpress Codex中的wp_ajax_(action)。

--编辑--

如果你想快速开始(不是我们都这样),这个代码片段几乎可以完成你想要的一切,把它放在functions.php中,然后从javascript中调用/wp-admin/admin-ajax.php。在ajax调用中,请确保添加一个变量action并用my_action填充它,以便本例正常工作。

add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
    $data['output'] = $_REQUEST['username'];
    echo json_encode($data);
    wp_die();
}