$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>jquery - Can't access data from database using AJAX|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

jquery - Can't access data from database using AJAX

matteradmin11PV0评论

I'm trying to make an AJAX call for the first time in wordpress. I followed some tutorial and have came to this point so far. But when I'm trying to console.log the data I get from database inside my AJAX call, I get this error:

Uncaught ReferenceError: data is not defined

Code:

functions.php

function my_ajax_handler(){
    global $wpdb;
    $name = $wpdb->get_results("SELECT * FROM username");

        echo $name;
}

add_action( 'wp_ajax_call_my_ajax_handler', 'my_ajax_handler' );
add_action( 'wp_ajax_nopriv_call_my_ajax_handler', 'my_ajax_handler' );

function enqueue_styles() {
    wp_enqueue_style("child-style", get_template_directory_uri() .'/style.css');
    wp_enqueue_style("parent-style", ".1.3/css/bootstrap.min.css");
    wp_enqueue_script("jquery", '.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous');
    wp_enqueue_script( 'my-ajax-script', get_stylesheet_directory_uri() . '/path/to/script.js', array('jquery') );
    wp_localize_script( 'my-ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

add_action("wp_enqueue_scripts", "enqueue_styles");

quiz-loged-page.php

<script>
jQuery.ajax({
    type:"POST",
    url: my_ajax_object.ajax_url,
    data: { 'action': 'call_my_ajax_handler' }
}).done(function() {
  console.log(data);
});

</script>

I'm trying to make an AJAX call for the first time in wordpress. I followed some tutorial and have came to this point so far. But when I'm trying to console.log the data I get from database inside my AJAX call, I get this error:

Uncaught ReferenceError: data is not defined

Code:

functions.php

function my_ajax_handler(){
    global $wpdb;
    $name = $wpdb->get_results("SELECT * FROM username");

        echo $name;
}

add_action( 'wp_ajax_call_my_ajax_handler', 'my_ajax_handler' );
add_action( 'wp_ajax_nopriv_call_my_ajax_handler', 'my_ajax_handler' );

function enqueue_styles() {
    wp_enqueue_style("child-style", get_template_directory_uri() .'/style.css');
    wp_enqueue_style("parent-style", "https://stackpath.bootstrapcdn/bootstrap/4.1.3/css/bootstrap.min.css");
    wp_enqueue_script("jquery", 'https://code.jquery/jquery-3.3.1.min.js"
  integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
  crossorigin="anonymous');
    wp_enqueue_script( 'my-ajax-script', get_stylesheet_directory_uri() . '/path/to/script.js', array('jquery') );
    wp_localize_script( 'my-ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}

add_action("wp_enqueue_scripts", "enqueue_styles");

quiz-loged-page.php

<script>
jQuery.ajax({
    type:"POST",
    url: my_ajax_object.ajax_url,
    data: { 'action': 'call_my_ajax_handler' }
}).done(function() {
  console.log(data);
});

</script>
Share Improve this question asked Nov 13, 2018 at 14:03 LimpulsLimpuls 3071 gold badge3 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Pass data as an argument in done(function(data) funtion.

<script>
jQuery.ajax({
    type:"POST",
    url: my_ajax_object.ajax_url,
    data: { 'action': 'call_my_ajax_handler' }
}).done(function(data) {
  console.log(data);
});

Post a comment

comment list (0)

  1. No comments so far