【问题标题】:get content from page through AJAX wordpress通过 AJAX wordpress 从页面获取内容
【发布时间】:2013-12-03 15:23:13
【问题描述】:

我正在使用 wordpress 主题,我想从另一个页面的 div 内的页面加载内容。

这是我想在 div 中加载的页面代码:

page-usercmets.php(这是一个自定义页面模板,我在其中检索当前用户的 cmets)

<?php

    get_header();

?>

    <div id="primary" class="row">

        <div id="content" class="span9" role="main">

    <!-- GET CURRENT USER -->

    <?php

    global $current_user;
    get_currentuserinfo();

    echo 'Username: ' . $current_user->user_login . '<br />';
    echo 'User display name: ' . $current_user->display_name . '<br />';
    ?>


   <!-- GET COMMENTS OF USER -->
    <?php 
    $args = array(
    'user_id' => $current_user->ID, // use user_id
    'post_type' => 'debate'
     );
    $comments = get_comments($args);

    ?>
     <ol class="commentlist">
            <?php
              wp_list_comments(
  array(
    'per_page' => 10, //Allow comment pagination
    'reverse_top_level' => false //Show the latest comments at the top of the list
  ), 
  $comments
);
            ?>
        </ol><!-- .commentlist -->
        </div><!-- #content .site-content -->

    </div><!-- #primary .content-area -->

<?php get_footer(); // This fxn gets the footer.php file and renders it ?>

我想在另一个页面的 div 中获取此页面的输出,我正在考虑使用 AJAX,代码如下:

更新:

     $.ajax({
     type: "GET",
     url:"http://www.mywebsite.co/cpt/user-comments",
     cache: false,
     dataType: 'html',
     success: function(data){
               $("#div").append(data);        //   <--- look here
    },
     error: function(){ },
     complete: function(){ }
    });

    });

稍后更新:我已通过使用 WORDPRESS *AJAX API* 解决了这个问题

这是正确的方法吗?

而且,我不知道应该如何添加网址,因为为我要检索的页面创建的永久链接是 "mywebsite.com/user-cmets" 谢谢!

【问题讨论】:

    标签: javascript php ajax wordpress jquery


    【解决方案1】:

    这是我的 ajax 函数...

    您发送您的 ajaxcall,当它返回成功代码时,获取数据并将其放入您的 div。

    $.ajax({
        type: "GET",
        url: yourUrl,
        cache: false,
        dataType: 'html',
        success: function(data){
                   $("#div").append(data);        //   <--- look here
        },
        error: function(){ },
        complete: function(){ }
    });
    

    【讨论】:

    • 我已经添加了这个网址:"mywebsite.com/user-comments",我得到的是:SyntaxError: Unexpected token :怎么了?
    • 你有错误成功吗?而不是'div.append.data'你能'alert(data)'......接收到的数据可能不是html,或者你的url不正确?这是文档:api.jquery.com/jQuery.ajax
    • 我在 URL 中添加了",因为它丢失并添加了警报而不是附加,现在我得到:XMLHttpRequest cannot load http://www.mywebsite.co/user-comments?_=1386084921073. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mywebsite.co' is therefore not allowed access.
    • 我会接受你的回答,因为这是正确的方法,但是解决我的问题的最佳方法是使用 wordpress ajax api。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    相关资源
    最近更新 更多