Add_action钩子未触发,因此 Ajax 返回 Undefined


Add_action hook not triggered, so Ajax returns Undefined

这是我第一次实现ajax。几天来,我一直在努力使用此示例 ajax 代码,在 stackoverflow 中搜索,阅读博客,尝试许多不同的解决方案,甚至发现了一些错误,但是。代码仍返回 $response = 未定义/0。

Wordpress 在页面中上传 ajax,

页面通过 ajax 调用与服务器交互并返回成功,但变量 $response 没有传递给 jquery,既没有传递给 response.status 也没有传递给 response.message。

事实上,$response在 Firefox 控制台中是 Undefined 或 0。

**经过所有这些检查,我发布了这个问题。Snake_Eyes和Froxz发现了一些错误,我纠正了这些错误。尽管如此,ajax 仍然为 $response 返回 Undefined。

我已经调查了Wordpress是否调用do_stuff_pp((。为此,我使用 PHP 函数 error_log(( 逐行检查了 do_stuff_pp(( 函数。分析表明 do_stuff_pp(( 函数永远不会运行。但我不知道为什么::(**

我正在使用jquery 1.5.1,WP 2.9.2和php 5.2.6

function my_init() {
    switch ($page_num) {
    case 4445: // enrollment page1
        $path = get_bloginfo('template_directory');
        $source = $path . "/js/ajax_test.js";
        wp_enqueue_script(
                $handle='this-is-my-ajax-handle',
                $source,
                $dependencies = array('jquery')
        );
        break;
    }

    switch ($page_num) {
    case 4445: // enrollment page1
        $data = array(
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('ajaxin_it'),
            'action' => 'ajaxin_it'
        );
        wp_localize_script('this-is-my-ajax-handle','ajaxYall', $data);     
        break;
    }
}
add_action('init', 'my_init');
// Define functions for ajax:
$my_action = 'ajaxin_it';
if (defined('DOING-AJAX') && DOING_AJAX) {
    // for loggedin users
    add_action('wp_ajax_' . $my_action, 'do_stuff_pp');
    // for non logged in users
    add_action('wp_ajax_nopriv_' . $my_action, 'do_stuff_pp');
}
function do_stuff_pp(){
    $response = array(
        'status' => 'error',
        'message' => "It's AJAX Y'all!"
    );
    // check is a legitimate call
    if(isset($_GET['nonce']) && wp_verify_nonce($_GET['nonce'], 'ajaxin_it')){
        $response = array(
            'status' => 'success',
            'message' => "It's AJAX Y'all!"
        );
    }
    header('Content: application/json');    
    echo json_encode($response);
    die();
}

这是.js:

jQuery(document).ready(function($){  
    $('#text_ajax_yall').click(function(){
        var $el = $(this);
    alert(ajaxYall.nonce);
    $.ajax({url:ajaxYall.ajaxurl,action:ajaxYall.action,nonce:ajaxYall.nonce,type:'GET',dataType:'json',success:function(response, textStatus, jqXHR){
        if( 200 == jqXHR.status && 'success' == textStatus ) {
            if( 'success' == response.status ){
                        $el.after( '<p style="color:green;">' + response.message+ '</p>' );
                    } else {
                        $el.after( '<p style="color:red;">' + response.message + '</p>' );
            }
        }
//      $el.after( '<p style="color:green;">' + response.message + '</p>' );
        console.log(response.message, textStatus, jqXHR );      
    }});
    });
});

造成这种情况的具体原因是什么,我该如何解决?

> response.prova 您尝试获取值,但正如我在 php 中看到的那样,您没有具有值证明的数组...尝试console.log只是响应喜欢这个

console.log(response);

在数组中,您有 2 个值"状态"和"消息">

所以基本上会response.status and response.message

伙计,你的PHP代码中没有定义prova属性。

你有这样的东西:

$response = array(
        'status' => 'error',
        'message' => "It's AJAX Y'all!"
    );

所以你必须打电话:

console.log(response.status);

 console.log(response.message);