doupingtang9627 2014-12-24 22:56
浏览 67

为什么我在Wordpress中从AJAX Call得到“0”响应

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:

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>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值