首页 > 解决方案 > AJAX 调用返回一个未定义的响应 php

问题描述

假设我有一个 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);
?>

标签: javascriptphpajaxwordpress

解决方案


推荐阅读