AJAX调用返回未定义的响应php

时间:2019-02-21 16:51:36

标签: javascript php ajax wordpress

让我们说我有一个php文件,它包含的只是下面的代码

<?
  $response = array(
    'btn' => '',
    'keys' => 0
  );

  if(isset($_POST['method'])){
    $response['btn'] = "<button>This is a button</button>";
  }

  echo json_encode($response);
?>

现在我像这样对这个php文件进行ajax调用

//Using wordpress ajax class
$.post('<?php echo admin_url('admin-ajax.php'); ?>', {action: 'key_bank', method: 'display'}, function(response) {
    console.log(response.btn);
});

未检查日志response.btn

这是怎么回事?

编辑:

这里是完整的脚本,以防万一。这是进行ajax调用的文件。

<?
/**
 * @package   Key Bank
 * @author    Joseph WIlliamson
 */

 //Secure file from direct access
if (! defined('WPINC')) {
    die("This file is protected from being accessed directly");
}

$response = array(
    'btn' => '',
    'keys' => 0
);

$currentUrl = get_bloginfo('url');

if(isset($_POST['video_id'])){
    $videoId = $_POST['video_id'];
    //Get all rows in database
    //Check to see if the user is logged in
    if(is_user_logged_in()){
        global $wpdb;

        //Current users ID
        $current_user_id = get_current_user_id();
        $current_user_affiliate_id = $wpdb->get_row("SELECT * FROM wp_uap_affiliates WHERE uid=" . $current_user_id);
        $query = $wpdb->get_row("SELECT * FROM wp_uap_referrals WHERE affiliate_id = ". $current_user_affiliate_id->id . " AND amount > 0");
        if(!empty($query)){
            $keys = (int) $query->amount;
            if($keys > 0){
                $newKeys = $keys - 1;
                $wpdb->update( 
                    'wp_uap_referrals', 
                    array('amount' => $newKeys), 
                    array( 'ID' => $query->id ), 
                    array( '%s' ), 
                    array( '%d' ) 
                );

                //Get current amount of votes on post
                $votes = get_post_meta($videoId,'contest-video-points',true);
                //Increase the votes by 1
                $votes += 1;
                //Update the post with the new value
                update_post_meta($videoId,'contest-video-points',$votes);

                $user_id = get_current_user_id();
                $user_id_to = get_post_meta((int)$_POST['video_id'],'contest-video-author',true);           
                setKeyData($user_id, $user_id_to );
            }
        }
    }
}

//Check to see if the user is logged in
if(is_user_logged_in()){
    global $wpdb;

    //Store variable for accessing keys in key bank
    $keys = 0;

    //Current users ID
    $current_user_id = get_current_user_id();
    $current_user_affiliate_id = $wpdb->get_row("SELECT * FROM wp_uap_affiliates WHERE uid=" . $current_user_id);

    $query = $wpdb->get_results("SELECT SUM(amount) AS totalAmount FROM wp_uap_referrals WHERE affiliate_id = ".$current_user_affiliate_id->id."");
    $keys = (int)$query[0]->totalAmount;

    $response['keys'] = $keys;

    if(isset($_POST['method'])){
        switch($_POST['method']){
            case "display":
                $response['btn'] = '<button class="btn btn-primary" onclick="location.href = \''.$currentUrl.'/donate/\'">Earn more</button>';
                break;
            case "video_modal":
                if($response["keys"] > 0){
                    $response['btn'] = '<button class="btn btn-primary use-key-bank-btn">Give key from key bank</button>';
                }else{
                    $response['btn'] = '<button class="btn btn-primary" onclick="location.href = \''.$currentUrl.'/donate/\'">Earn more</button>';
                }
                break;
        }
    }else{
        $response['btn'] = 'Invalid method header sent';
    }

} else {
    $response['btn'] = "<button class='btn btn-primary' onclick='location.href=".$currentUrl."/wp-login.php?redirect_to=".$currentUrl."/open-doors-challenge/'>Please login to give key.</button>";
}


echo json_encode($response);
?>

0 个答案:

没有答案