0

我无法使用 ajax 从 single.php 中的 div(宽度为特定 ID)获取内容到 index.php 中。现在我有这个代码并且什么都不返回 - “0”。

function loadcontent(type,id,slug, action) {
    jQuery.ajax({
        type:'POST', 
        url:srvars.ajaxurl, 
        data: { 
            action:'sr_get_content', 
            id:id, 
            type:type
        }, 
        success: function(response) {
            if ($(window).width() > 768) {
                $('#content .content-inner').hide();
                $('#content .content-inner').html(response);
                initialise('#content');
            }
            var checkwidth = $('.maincontent').width();
            $('#loading').delay(1000).fadeOut(fadeInOutSpeed, function(){
                if ($(window).width() > 768) {
                    if (checkwidth > 0) {
                        reorganizeIsotope('.masonry');
                        $('#content .content-inner').fadeIn(fadeInOutSpeed, function() { resize_jplayer(); reorganizeIsotope('.masonry'); });
                    } else {
                        $('.mainside').animate({'width':srvars.asidewidth+'%'}, animationSpeed, $easingType);
                        $('.mainside-bg').animate({'width':srvars.asidewidth+'%'}, animationSpeed, $easingType);
                        $('.maincontent').animate({'width':srvars.contentwidth+'%'}, animationSpeed, $easingType, function() {
                            reorganizeIsotope('.masonry');
                            $('#content .content-inner').fadeIn(fadeInOutSpeed, function() { resize_jplayer(); reorganizeIsotope('.masonry'); });
                        }); 
                    }
                } else {
                    $('#content .content-inner').hide();
                    $('#content .content-inner').html(response);
                    initialise('#content');
                    if ($(window).width() <= 480) { var topscroll = jQuery('header').height(); } else { var topscroll = 0; }
                    $('html, body').animate({scrollTop: topscroll+'px'}, animationSpeed, $easingType);
                    reorganizeIsotope('.masonry');
                    $('#content .content-inner').slideDown(fadeInOutSpeed, $easingType, function() { 
                        resize_jplayer(); 
                        reorganizeIsotope('.masonry'); 
                    });
                }
            });
        }
    });
}
$("body").on("click", 'a.loadcontent', function() { 
    var href = $(this).attr('href');
    var type = $(this).data('type');
    var id = $(this).data('id');
    var slug = $(this).data('slug');

    if(window.location.hash.substr(1) == slug) { 
        $('html, body').animate({scrollTop: 0}, animationSpeed, $easingType);
    } else {
        window.location.hash = slug;    // set the hash
        //history.pushState({page:href}, href, href);
        loadcontent(type,id,slug,false);
    }

    return(false);
});

并来自functions.php

add_action('wp_ajax_nopriv_sr_get_content', 'loadAjaxPosts');
add_action('wp_ajax_sr_get_content', 'loadAjaxPosts');
function loadAjaxPosts() { 
    switch($_REQUEST['type']) {
        case 'portfolio':
            $output = sf_portfolio_items($_REQUEST['cat'], $_REQUEST['count'], $_REQUEST['page'], true);
        break;
        case 'blog':
            $output = sf_blog_items($_REQUEST['cat'], $_REQUEST['count'], $_REQUEST['page'], true);
        break;
        default:
            $output = 'Incorrect type specified, please contact support.';
        break;

    }
    $output=json_encode($output);
    if(is_array($output)) {
        print_r($output);   
    } else {
        echo $output;
    }
    die;    
}

如何传输内容?谢谢

4

1 回答 1

0

您的loadContent()函数的成功处理程序绝对什么都不做。因此,无论您的 php 脚本发回什么,都将被丢弃。

你需要类似的东西:

    success: function(response) {
       $('#whatever').html(response);
    }

另请注意,您isarray($output)将永远不会工作。json_encode() 返回一个STRING ...绝不是一个数组。

于 2013-02-20T14:35:52.787 回答