0

这是我第一次实现ajax。几天来,我一直在努力处理这个示例 ajax 代码,在 stackoverflow 中搜索,阅读博客,尝试了许多不同的解决方案,甚至发现了一些错误,但是.. 代码仍然返回 $response = Undefined/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 );      
    }});

    });

});

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

4

2 回答 2

1

response.prova您尝试获取该值,但正如我在 php 中看到的那样,您没有带有 value prova 的数组...尝试像这样console.log响应

console.log(response);

在数组中,您有 2 个值“状态”和“消息”

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

于 2013-10-04T07:45:16.243 回答
1

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

你有类似的东西:

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

所以你必须打电话:

console.log(response.status);

或者

 console.log(response.message);
于 2013-10-04T07:47:41.073 回答