为什么我得到" 0"来自Wordpress的AJAX Call的响应

时间:2014-12-24 22:56:10

标签: php jquery ajax wordpress twitter

我读了很多答案并尝试了很多东西,但我仍然得到0回应并且遇到问题。我正在尝试创建一个按钮供我的用户下载文件,当它是免费的,但在推特之后,我正在使用" Easy Digital Downloads" wp插件。

我的代码如下:

add_action( 'wp_enqueue_scripts', 'ritzy_scripts' );
function ritzy_scripts() {
        wp_enqueue_script( 'custom', get_stylesheet_directory_uri() . '/js/custom.js', array( 'jquery' ), CHILD_THEME_VERSION, true );
        wp_localize_script( 'custom', 'ritzy', array( 
                'ajaxurl' => admin_url( 'admin-ajax.php' ),
                'nonce' => wp_create_nonce( $GET['download_id'] ),
            ) 
        );
}

/**
 * AJAX
 */
add_action('wp_ajax_ritzy', 'ritzy');
add_action('wp_ajax_nopriv_ritzy', 'ritzy');
function ritzy() {    
    check_ajax_referer( $GET['download_id'], 'nonce' );

    // i think may be thats how i can send the link or button

    echo edd_get_purchase_link() . '<span id="share_to_download"></span>';
    die();
}

这是我的jQuery

jQuery(document).ready(function($) {

    //alert( $( '#tweet-to-download-wrap').length );

    if( $( '#tweet-to-download-wrap').length ) {
        $.post( ritzy.ajaxurl, {
            action: 'ritzy',
            theme: $('#tweet-to-download-wrap').attr('data-theme'),
            nonce: ritzy.nonce
        }, function(data) {
            $('#tweet-to-download-wrap').html(data);
            $('.edd-add-to-cart').on('click', function(e){
                e.preventDefault();
            });

            alert( $('.edd-add-to-cart').length );

            // Load twitter
            if( $('.tweet-to-download').length ){

                // First, load the widgets.js file asynchronously
                window.twttr = (function (d,s,id) {
                  var t, js, fjs = d.getElementsByTagName(s)[0];
                  if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
                  js.src="https://platform.twitter.com/widgets.js";
                  fjs.parentNode.insertBefore(js, fjs);
                  return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
                }(document, "script", "twitter-wjs"));

                twttr.ready(function(twttr){
                    twttr.widgets.createShareButton(
                        $('#tweet-to-download-wrap').attr('data-url'),
                        document.getElementById('tweet-to-download'),
                        function(el) {},
                        {
                            count: 'horizontal',
                            text: $('#tweet-to-download-wrap').attr('data-content'),
                            related: 'ritzythemes'
                        }
                    );

                    twttr.events.bind('tweet', function(intentEvent){
                        if (!intentEvent) return;
                        $.post(ritzy.ajaxurl, {
                                action: 'share_to_download',
                                theme: $('#tweet-to-download-wrap').attr('data-theme'),
                                nonce: ritzy.nonce
                            }, function( data ){
                            window.location.reload(true);
                        });
                    });
                });
            }

        });
    }

});

响应标题

Cache-Control   no-cache, must-revalidate, max-age=0
Connection  Keep-Alive
Content-Length  36
Content-Type    text/html; charset=UTF-8
Date    Wed, 24 Dec 2014 23:05:37 GMT
Expires Wed, 11 Jan 1984 05:00:00 GMT
Keep-Alive  timeout=5, max=91
Pragma  no-cache
Server  Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11
Set-Cookie  edd_items_in_cart=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/wp/
X-Content-Type-Options  nosniff
X-Frame-Options SAMEORIGIN
X-Powered-By    PHP/5.5.11
X-Robots-Tag    noindex

这个回应

<span id="share_to_download"></span>

运行此脚本后,HTML为

<span id="tweet-to-download-wrap" data-content="Sparking: A free WordPress theme by @ritzythemes" data-url="http://localhost/wp/themes/sparkling/" data-theme="705">
<span id="share_to_download"></span>
</span>

0 个答案:

没有答案