javascript - 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);
?>
解决方案
推荐阅读
- javascript - Google Calendar API:检查日历上是否存在事件
- c# - 无法在类型参数化方法中将子类隐式转换为父类
- c# - 初始化通用类型属性
- bluetooth - 是否可以在不配对的情况下通知 GATT 事件?
- javascript - 为什么我的 js 代码在 Laravel 8 的 Livewire 中执行点击事件时会暂停?
- amazon-web-services - 服务目录RDS MS SQL产品-如何解决亚马逊云形成异常用户
- python - Pygame:如何一次又一次地从左到右移动图像
- python-idle - 如何在 Mac 中找到 IDLE 源代码(idlelib)?
- javascript - 如何使用 joi 添加自定义错误消息?
- python - 无法使用 social-auth-app-django 断开与 Google OAuth2 的连接