将PHP URL添加到 AJAX 请求只能在WordPress文件夹之外工作

zf2sa74q  于 2023-09-29  发布在  PHP
关注(0)|答案(1)|浏览(115)

我试图在我 AJAX 请求中添加一个php url,但它不起作用。
代码:
这是可行的(但不是我的想法)。它使用public_html文件夹中的文件。
场景1:
当我添加了正确的url到我添加了php文件的位置时,它不起作用,分页停止。

  1. <script>
  2. // Home Pagination
  3. $(document).ready(function () {
  4. // Initialize with the current page
  5. var currentPage = <?php echo $current_page; ?>;
  6. // Load matches for the initial page
  7. loadMatches(currentPage);
  8. $('.pagination-link').click(function (e) {
  9. e.preventDefault();
  10. var page = $(this).data('page');
  11. loadMatches(page);
  12. });
  13. // Toggle the visibility of tables when a button is clicked
  14. $('.toggle-table').click(function () {
  15. $(this).next('.match-table').toggle(); // Toggle the associated table
  16. });
  17. function loadMatches(page) {
  18. $.ajax({
  19. type: 'GET',
  20. url: 'wp-content/plugins/matches/ajax-matches.php',
  21. data: { page: page }, // Send the page parameter
  22. success: function (data) {
  23. $('#matches-container').html(data);
  24. },
  25. error: function () {
  26. alert('Error loading matches.');
  27. }
  28. });
  29. }
  30. });
  31. </script>

场景二:

  1. <script>
  2. // Home Pagination
  3. $(document).ready(function () {
  4. // Initialize with the current page
  5. var currentPage = <?php echo $current_page; ?>;
  6. // Load matches for the initial page
  7. loadMatches(currentPage);
  8. $('.pagination-link').click(function (e) {
  9. e.preventDefault();
  10. var page = $(this).data('page');
  11. loadMatches(page);
  12. });
  13. // Toggle the visibility of tables when a button is clicked
  14. $('.toggle-table').click(function () {
  15. $(this).next('.match-table').toggle(); // Toggle the associated table
  16. });
  17. function loadMatches(page) {
  18. $.ajax({
  19. type: 'GET',
  20. url: 'ajax-matches.php',
  21. data: { page: page }, // Send the page parameter
  22. success: function (data) {
  23. $('#matches-container').html(data);
  24. },
  25. error: function () {
  26. alert('Error loading matches.');
  27. }
  28. });
  29. }
  30. });
  31. </script>

我试过很多次了。我试过但没有成功的一些方法:添加变量var ajax_target = "<?php echo plugins_url('/matches/ajax-matches.php', __FILE__); ?>";
你的帮助会有很长的路要走!

doinxwow

doinxwow1#

尝试添加一个/或使用完整的URL,像这样:

  1. url: '/wp-content/plugins/matches/ajax-matches.php'

或者这个

  1. url: 'https://example.com/wp-content/plugins/matches/ajax-matches.php',

我不能发表评论(没有足够的声誉),这个答案可能会导致我的惩罚,但仍然很高兴能帮助!

相关问题