gpt4 book ai didi

javascript - 将 POST 更改为 GET。现在 Ajax 返回多个帖子

转载 作者:行者123 更新时间:2023-11-28 07:30:51 25 4
gpt4 key购买 nike

当我使用 POST 而不是 GET 进行调用时,等待时间非常长。因此,由于我实际上没有发送任何数据,所以我尝试切换到 GET。速度提高了一点,但现在它不再抓取我需要的一个帖子,而是返回多个帖子。我猜我需要在函数中以某种方式改变循环,但我不知道该怎么做。有人可以帮我吗?

通话

$.ajax({
type: 'get',
url: ajaxURL,
data: {'action': 'load-content', post_id: post_id },
success: function(response) {

// Wait until all images are loaded
$('#project-container').html(response).imagesLoaded().then(function() {

// Fire again to rearrange the slide in the DOM
resize();

// Remove all 'hover' classes
$('article.project').removeClass('hover-sticky grayscale-sticky');
$('article.project img').removeClass('nohover');

// Remove the loading icon
$('.loading-icon').remove();

// If the user has scrolled...
if ($(window).scrollTop() !== 0) {

// First scroll the page to the top
$('html, body').animate({
scrollTop : 0
},400, function() {

matchContainerHeight();
projectStyles();

});

// If the user has not scrolled...
} else {

matchContainerHeight();
projectStyles();

}

return false;
});
}
});

函数

<?php
/**
* Ajax functions
*/

// Return the post content to the AJAX call
function my_load_ajax_content () {

$args = array(
'p' => $_POST['post_id'],
'post_type' => 'projects'
);

$post_query = new WP_Query( $args );
while( $post_query->have_posts() ) : $post_query->the_post(); ?>

<div class="post-container">
<div id="project-left-content">
<?php

the_title( '<h1 class="entry-title">', '</h1>' );
the_content();

if( get_field('client') ): ?>
<div class="client">
Client(s): <?php the_field('client'); ?>
</div>
<?php endif; ?>

<div class="project-cats">
<?php
$cat_names = wp_list_pluck( get_the_category(), 'cat_name');
echo join( ', ', $cat_names );
?>
</div>

<?php if( get_field('url') ): ?>
<div class="project-link">
<a class="first after" href="http://<?php the_field('url'); ?>" target="_blank"><?php the_field('url'); ?></a>
</div>
<?php endif; ?>
</div>
<div id="project-right-content">

<?php if( have_rows('slides') ): ?>

<div id="slider">

<!-- Slider Setup -->
<?php if( have_rows('slides') ):
$slideNumber = 0;
while ( have_rows('slides') ) : the_row();
$slideNumber++;
?>

<input type="radio" name="slider" id="slide<?php echo $slideNumber; ?>">

<?php endwhile;endif; ?>

<!-- Slide -->
<?php if( have_rows('slides') ): ?>
<div id="slides">
<div id="overflow">
<div class="inner">

<?php if( have_rows('slides') ):
while ( have_rows('slides') ) : the_row();

$slideImage = get_sub_field('slide_image');
?>

<article>
<img src="<?php echo $slideImage; ?>" alt="<?php the_title(); ?>">
</article>

<?php endwhile;endif; ?>

</div><!-- #inner -->
</div><!-- #overflow -->
</div><!-- #slides -->

<?php endif; ?>

<!-- Controls -->
<?php if( have_rows('slides') ):
$slideNumber = 0;
?>
<div id="active">

<?php while ( have_rows('slides') ) : the_row();
$slideNumber++;
?>
<label for="slide<?php echo $slideNumber; ?>"></label>
<?php endwhile; ?>

</div><!-- #active -->
<?php endif; ?>
</div><!-- #slider -->
<?php endif; ?>
</div><!-- #project-right-content -->
</div><!-- .post-container -->

<?php
endwhile;
wp_reset_postdata();
wp_die();
}

add_action ( 'wp_ajax_nopriv_load-content', 'my_load_ajax_content' ); // when the user is logged in
add_action ( 'wp_ajax_load-content', 'my_load_ajax_content' ); // when the user is not logged in

最佳答案

改变

$args = array(
'p' => $_POST['post_id'],
'post_type' => 'projects'
);

$args = array(
'p' => $_GET['post_id'],
'post_type' => 'projects'
);

因为你的ajax是GET而不是POST了!

关于javascript - 将 POST 更改为 GET。现在 Ajax 返回多个帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29142030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com