1

我有下一个类 php:

class Job{

    public $resultado;
    public $busqueda;
    public $wpdb;

    public function __construct($busqueda){
        global $wpdb;
        $this->busqueda = $busqueda;
        $this->resultado = array();
        $this->wpdb = $wpdb;
    }   

    public function search_job($resultado){
        $tablepost =$this->wpdb->prefix.'posts';
        $query = "SELECT * from $tablepost WHERE post_type = 'jobman_job' and post_status='publish';";



        if (isset($wpdb)) {
            global $wpdb;
            $result = $wpdb->get_results($query);
        }else{
            $result = $this->wpdb->get_results($query);
        }



        print_r($result);
    }

}

这发现没问题。现在我想用 jQuery .ajax 调用 search_function 我试试这个:

(function($){

    $('#searchjob').keypress(function(e){
        var search = $.trim($('#searchjob').val());

        if (e.which == "13") {

            $.ajax({
                beforeSend: function(){
                    //$('#loading').html(<img src="rutagif" alt="loading" />);
                },
                url:"../wp-content/themes/SUP2012/class/job.php",
                data:{method: 'search_job', data:{resultado: 'hola'}},
                type: "POST",
                success: function(data){

                }

            }); 
        };

    });

 })(jQuery);

url 参数响应正常(200 OK),但不检索信息。任何的想法?

4

1 回答 1

1

要提出ajax请求,wordpress您应该使用 infunctions.php

add_action('wp_ajax_nopriv_your_function_name', 'your_function_name');
add_action('wp_ajax_your_function_name', 'your_function_name');
function your_function_name()
{
    // do anything here and echo the result
    $data_from_ajax=$_POST['data_to_send_to_server'];
    die(); // last line 
}

javascript应该看起来像

$('#searchjob').keypress(function(e){
    var your_data = $.trim($(this).val());
    if (e.which == "13") {
        $.ajax({
            type:"POST",
            url: "./wp-admin/admin-ajax.php", // if this file is in a subfolder in your themes folder
            data: {
                action:'your_function_name', // the function name you used in functions.php
                data_to_send_to_server:your_data // retrieve from $_POST in php
            },
            success:function(data){
                // do something with data
            }
        });
    }
});
于 2012-10-20T05:56:30.373 回答