Admin-ajax.php正在死亡“0”,没有处理功能

时间:2013-11-11 作者:Sheixt

I am trying to learn how to process AJAX requests in Wordpress the correct way. To do this I am adapting this tutorial to create a super simple AJAX request to place the ID of a post (from a link) into my page content.

The Logic

  1. When the #our-work a links are clicked [js]
  2. Get the post ID (from the data-id attribute) & store it as postID variable [js]
  3. Pass postID via an AJAX (using the WP\'s admin-ajax.php file) [js]
  4. The example_ajax_request function will pick up the ID & simply echo it [php]
  5. If successful, append the server response to the #hero div.

I realise this has no benefit but once I have that working I will amend the function to serve a real purpose.

My Code

Here is a copy of the function I have created in the plugins folder:

wp_enqueue_script( \'my-ajax-request\', plugin_dir_url( __FILE__ ) . \'acl-plugin.js\', array( \'jquery\' ) );

wp_localize_script( \'my-ajax-request\', \'MyAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );

function example_ajax_request() {
    if ( isset($_REQUEST) ) {
        $postID = $_REQUEST[\'postID\'];
        echo $postID;
    }
    die();
}
add_action( \'wp_ajax_nopriv_example_ajax_request\', \'example_ajax_request\' );

And here is a copy of the JS file

jQuery(document).ready(function($) {
    $(\'#our-work a\').click( function() {
        var postID = $(this).attr("data-id");

        $.ajax({
            url: MyAjax.ajaxurl,
            data: {
                \'action\':\'example_ajax_request\',
                \'postID\' : postID
            },
            success:function(data) {
                $(\'#hero\').append( "Well this seems to work" + data );
            },
            error: function(errorThrown){
                console.log("This has thrown an error:" + errorThrown);
            }
        });
        return false;
    });
});

The Problem

Upon clicking the link the JS does fire but yields the following response:

<div id="hero">
    Well this seems to work 0
</div>

Using an alert I know the ID is being picked up before the AJAX request. So the problem is in my function. To find out more, I (temporarily) tweaked WP\'s admin-ajax.php file, so that I could find out which die(); was yielding the response of "0".

It is the very last one in the file which I thought wouldn\'t fire as I have a die(); command in my own function. Can someone please point out where I am going wrong?

3 个回复
SO网友:Sheixt

这是我能自豪地说的罕见的一次。。。这里没有任何问题!

服务器返回的原因0 是因为我登录了!这个wp_ajax_nopriv_example_ajax_request 仅适用于以下用户not 已登录。注销后,这一切正常。

因此,如果您希望执行相同的操作,只需确保在函数下面有两个操作:

add_action( \'wp_ajax_example_ajax_request\', \'example_ajax_request\' );
add_action( \'wp_ajax_nopriv_example_ajax_request\', \'example_ajax_request\' );

SO网友:Mark Cicchetti

另一件有趣的事情是Wordpress有自己的die(),称为wp\\u die()。我在AJAX调用结束时得到了0,直到我也找到了它。

echo(json_encode($return));
wp_die();
还有wp_send_json_successwp_send_json_error. 所有这些都返回您所期望的结果,而不带尾随的0。

wp_send_json_success($data);
将作为json返回,并带有数据元素

success: function(trn) {
    console.log(trn.data)
}

SO网友:Yuvraj Khavad

使用wp_die(); 代替die(); 在功能中。

add_action( \'wp_ajax_nopriv_example_ajax_request\', \'example_ajax_request\' );
add_action( \'wp_ajax_example_ajax_request\', \'example_ajax_request\' );
function example_ajax_request() {

    // Your code goes here.        

    wp_die();
}

结束

相关推荐

在WordPress AJAX API中使用主干

我试图使用主干构建一个简单的TODO插件,但遇到了AJAX API的问题。为了获得正确的AJAX回调,必须像下面这样传入“action”参数:管理ajax。php?操作=获取\\u待办事项对于主干中的GET方法来说,它工作得很好。但是,使用DELETE主干时,默认情况下将使用如下URL:管理ajax。php?操作=获取待办事项/9其中,“9”是要删除的todo的ID。通过添加这个ID,它中断了WordPress端的AJAX调用。我可以手动覆盖每个方法的URL,但我想知道是否有更优雅的方法让AJAX API