AJAX请求未返回结果

时间:2014-07-08 作者:chinna

我使用自定义插件在wordpress管理端显示值。。

我的Jquery html文件是

<script type=\'text/javascript\'>
                    jQuery(document).ready(function(){
                    jQuery("#searchsubmit").click( function(e) {
                    e.preventDefault();
                    var data = jQuery("#eventname").val();
                    var city =  jQuery("#city").val();
                    var date =  jQuery("#datepicker").val();

                    jQuery.ajax({
                            type:\'POST\',
                            url: \'./admin-ajax.php\',
                            data:{action:\'my_action\',test:data,test1:city,test2:date},
                            dataType: \'html\',                                   
                            success: function(response) {
                                alert(response);
                            }
                           });
                            });

                    });
                </script>
我的插件文件是

add\\u action(“wp\\u ajax\\u my\\u action”,“my\\u action\\u callback”);

function my_action_callback(){

        $name = $_POST[\'test\'];
        $city = $_POST[\'test1\'];
        $date = $_POST[\'test2\'];

        if ($date != \'\') {
            $enddate = date("Y-m-t", strtotime($date));
            $dates_sql = " AND start_date <= \'$date\' ";
            }
            if ($city != \'\' && $city != -1) 
            $destination_sql = "AND venue = \'$city\'";
            if ($name != \'\' && $name != -1) 
                $origincity_sql = "AND event_name = \'$name\'";

        $query = " SELECT * FROM <table name> WHERE event_id!=\'\' " . $dates_sql . " " . $destination_sql . " " . $origincity_sql . " ORDER BY start_date DESC ";

        $results = $wpdb->get_results($query) or die(mysql_error());    

        foreach ($results as $row)
        {
            $message  = "<tr><td align=\'center\' id = \'username\'>".$row->username."</td><td align=\'center\' id = \'eventname\'>".$row->event_name."</td><td align=\'center\' id = \'venue\'>". $row->venue."</td><td align=\'center\' id = \'email\'>".$row->email."</td><td align=\'center\' id = \'misseddays\'>".$row->missed_days."</td><td align=\'center\' id = \'paymentstatus\'>".$row->payment_mode."</td><td align=\'center\' id = \'amountpaid\'>".$row->amount_paid."</td><td align=\'center\' id = \'amountbalance\'>".$row->amount_balance."</td><td align=\'center\' id = \'mailsent\'>".$row->mail."</td></tr>";
        }  

        echo $message;
        exit();
    }
尽管jquery html文件返回-1作为响应,但我没有将响应输入到jquery html文件中。要进行任何更改,请帮助。今天在这件事上卡住了。

1 个回复
最合适的回答,由SO网友:cybmeta 整理而成

您没有正确使用Wordpress ajax API,请参阅Ajax in Plugins. Tha action参数为定义函数回调wp_aja_* 操作挂钩,而不是文件名:

<?php
add_action( \'wp_ajax_my_action\', \'my_action_callback\' );
//Uncomment next line if you want the ajax action enable in the frontend
//add_action( \'wp_ajax_nopriv_my_action\', \'my_action_callback\' );
function my_action_callback() {
    global $wpdb;
    $name = $_POST[\'test\'];
    $city = $_POST[\'test1\'];
    $date = $_POST[\'test2\'];

    if ($date != \'\') {
        $enddate = date("Y-m-t", strtotime($date));
        $dates_sql = " AND start_date <= \'$date\' ";
    }
   if ($city != \'\' && $city != -1) {
        $destination_sql = "AND venue = \'$city\'";
   }
   if ($name != \'\' && $name != -1) {
            $origincity_sql = "AND event_name = \'$name\'";
   }

   $query = " SELECT * FROM <table name> WHERE event_id!=\'\' " . $dates_sql . " " . $destination_sql . " " . $origincity_sql . " ORDER BY start_date DESC ";

    $results = $wpdb->get_results($query) or die(mysql_error());    

    foreach ($results as $row) {
        $message  = "<tr><td align=\'center\' id = \'username\'>".$row->username."</td><td align=\'center\' id = \'eventname\'>".$row->event_name."</td><td align=\'center\' id = \'venue\'>". $row->venue."</td><td align=\'center\' id = \'email\'>".$row->email."</td><td align=\'center\' id = \'misseddays\'>".$row->missed_days."</td><td align=\'center\' id = \'paymentstatus\'>".$row->payment_mode."</td><td align=\'center\' id = \'amountpaid\'>".$row->amount_paid."</td><td align=\'center\' id = \'amountbalance\'>".$row->amount_balance."</td><td align=\'center\' id = \'mailsent\'>".=$row->mail."</td></tr>";
    }  
    header(\'Content-Type: application/json\');
    echo json_encode($message);
    exit;
}

?>
您还可以查看wp_send_json, wp_send_json_errorwp_send_json_success.

结束

相关推荐

Ajax contact form return 0

我有一个联系人表单,我想使用ajax提交。似乎要发送数据,但唯一检索到的是0。HTML表单:<form method=\"post\" action=\"http://www.codetocode-developments.com/ela/wp-admin/admin-ajax.php\" name=\"contactform\" id=\"contactform\"> <fieldset> &l