weixin_33694620 2019-12-01 18:40 采纳率: 0%
浏览 250

Wordpress Ajax获取Json数据

I have A issue with AJAX in Wordpress. Without AJAX and direct PHP out I can see the Json data. But with the AJAX call the data is not showing.

This is my code

//<![CDATA[
jQuery(document).ready(function($) {
    select();
    function select(){
      var data = {
        'action': 'select',
        'todo': 'catselect',
      };

      jQuery.post(ajaxurl, data, function(response) {
        $.each(data, function (key, value) {
          console.log(value.catname);
        });    
      });
    }
});
//]]>

And the PHP code below

<?php
global $wpdb;
$cats_table = $wpdb->prefix . "jb_menu_groups";
function select() { 
    if($_POST['todo'] == "catselect"){
        $resultArray    = array();
        $connection     = mysqli_connect($wpdb->dbhost, $wpdb->dbuser, $wpdb->dbpassword, $wpdb->dbname);
        $sql            = "SELECT catname, catdescription FROM $cats_table";
        $result         = mysqli_query($connection, $sql); 
        header('Content-type: application/json; charset=utf-8');
        while($row = mysqli_fetch_assoc($result)){
            $resultArray[] = $row;
        }
            echo json_encode($resultArray);
    }
    wp_die();
}
add_action('wp_ajax_select', 'select');
?>

I want catname and catdescription as output

Who can help me with this?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划