Why I am getting “0” response from AJAX Call in WordPress

I read alot of answers and tried alot of things but I am still getting 0 response and having problem. I am trying to create a button for my users to download file when its free but after tweeting and I am using “Easy Digital Downloads” wp plugin.

my codes are following:

Read More
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();
}

and here is my 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);
                        });
                    });
                });
            }

        });
    }

});

RESPONSE HEADER

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

And this response

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

After running this script the HTML is

<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>

Related posts

Leave a Reply