【问题标题】:Why are there no problems with Ajax on the desktop, but have on the phone?为什么Ajax在桌面上没有问题,而在手机上却有?
【发布时间】:2020-10-11 09:17:21
【问题描述】:

网站上有一个按钮,当您单击该按钮时,会加载新帖子。在桌面上一切正常,但在手机上就坏了。

XHR failed loading: POST "https://test-5.vbbn.in/wp-admin/admin-ajax.php".
send @ jquery.js?ver=1.12.4-wp:4
ajax @ jquery.js?ver=1.12.4-wp:4
n. @ jquery.js?ver=1.12.4-wp:4
(anonymous) @ (index):1732
dispatch @ jquery.js?ver=1.12.4-wp:3
r.handle @ jquery.js?ver=1.12.4-wp:3
    add_action( 'wp_footer', 'my_action_javascript' );

   function my_action_javascript() { ?>
     <script>
           jQuery(document).ready(function($) {
           var page = 2;
   
           var post_count = jQuery('#posts').data('count');
   
           var ajaxurl = "<?php echo admin_url('admin-ajax.php');?>"
                jQuery('#load_more').click(function () {
   
                    
                       var data = {
                           'action': 'my_action',
                           'page': page
                       };
   
   
                       jQuery.post(ajaxurl, data, function(response) {
                           jQuery('#posts').append(response);
   
                           if (post_count == page){
                               jQuery('#load_more').hide();
   
                           }
   
                           page++;
                       });
                });
           });
     </script> <?php
   }
   
   
   
   add_action( 'wp_ajax_my_action', 'my_action' );
   
   function my_action() {
     $args = array(
       'post_type'   => 'post',
       'category_name'=> 'nouvelles',
       'paged' => $_POST['page'],
     );
   $the_query = new WP_Query( $args ); ?>
   
     <?php if ( $the_query->have_posts() ) : ?>
   
         <?php 
       while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

尝试添加e.preventDefault,以避免重启不写POST然后将事件传递给函数,但它表明e.preventDefault不是一个函数。

网站 - https://test-5.vbbn.in/ 希望得到任何帮助。谢谢

【问题讨论】:

    标签: javascript php jquery ajax wordpress


    【解决方案1】:

    您正在尝试使用管理员 ajax 访问 url。但 url 没有返回 200 作为响应。它返回 400(not found) 。我刚刚通过单击按钮测试了 url。请看下面我附上的截图。

    现在解决问题并确认您的操作可用于 admin-ajax

    【讨论】:

    • 你能告诉我如何授予对 admin-ajax.php 的访问权限,因为当我登录时,一切正常吗?
    • 但是又出现了一个新问题,由于某种原因关闭banner的脚本在手机上运行10-15秒后(
    • 可以分享链接吗?
    猜你喜欢
    • 2023-04-06
    • 1970-01-01
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多