Why Ajax Doesn't Work?

时间:2020-12-24 作者:DokaPavel

我在WordPress的根目录下有一个单独的页面,用于处理某些数据,其中包含;wp负载。php“;已连接到它的文件。

<?php

  require_once( dirname(__FILE__) . \'/wp-load.php\' );
  require_once( dirname(__FILE__) . \'/wp-admin/includes/admin.php\' );

  add_action( \'wp_ajax_example_ajax_request\', \'example_ajax_request\' );
  add_action( \'wp_ajax_nopriv_example_ajax_request\', \'example_ajax_request\' );

  function example_ajax_request() {
    if ( isset( $_REQUEST ) ) {
      $postID = $_REQUEST[\'postID\'];
      print $postID;
    }
    wp_die();
  }

?><!doctype html>
<html lang="ru" dir="ltr">

<head>
  <meta charset="utf-8">
  <title>Outside page</title>

  <script src="https://yastatic.net/jquery/3.3.1/jquery.min.js"></script>

  <script>
    var ajaxurl = "<?php print admin_url( \'admin-ajax.php\' ); ?>";
  </script>

  <script type="text/javascript">
    jQuery( document ).ready( function( $ ) {
      $( \'#our-work a\' ).click( function( e ) {
        e.preventDefault();

        var postID = $( this ).attr( \'data-id\' );

        $.ajax({
          url: \'./wp-admin/admin-ajax.php\',
          data: {
            \'action\' : \'example_ajax_request\',
            \'postID\' : postID
          },
          success : function( data ) {
            $( \'#hero\' ).append( \'Well this seems to work\' + data );
          },
          error : function(errorThrown){
            console.log( \'This has thrown an error:\' + errorThrown );
          }
        });
        return false;
      });
    });
  </script>

</head>

<body>

  <p id="our-work">
    <a data-id="123" href=".">Get and push!</a>
  </p>

  <div id="hero"></div>

</body>

</html>
很遗憾,我收到了错误:

enter image description here

为什么Ajax代码在这里不起作用?

是否需要连接其他文件?

2 个回复
SO网友:Rup

您应该将此代码移到新插件中,例如,将此文件另存为wp内容/插件/示例ajax处理程序。php

<?php
/*
Plugin Name: Example AJAX handler
Description: Handle example_ajax_request admin-ajax requests
Version: 0.0.1
*/

add_action( \'wp_ajax_example_ajax_request\', \'example_ajax_request\' );
add_action( \'wp_ajax_nopriv_example_ajax_request\', \'example_ajax_request\' );

function example_ajax_request() {
  if ( isset( $_REQUEST ) ) {
    $postID = $_REQUEST[\'postID\'];
    print $postID;
  }
  wp_die();
}
然后,您需要从管理站点的插件菜单中激活它。那么它应该会起作用!请参见Plugin Basics documentation.

SO网友:li bing zhao

更改打印$postID;回显$postID;

在函数中输入以下代码。php(在主题文件夹中)

添加\\u操作(“wp\\u ajax\\u example\\u ajax\\u request”,“example\\u ajax\\u request”);函数示例\\u ajax\\u request(){
if(isset($\\u request)){$postID=$\\u request[\'postID];echo$postID;}wp\\u die();}wp\\u die();}

3。出于测试目的,您需要启用日志功能,将下面的代码放入您的wp配置中。根文件夹中的php

    define( \'WP_DEBUG\', true );
    // Enable Debug logging to the /wp-content/debug.log file
    define( \'WP_DEBUG_LOG\', true );
    define( \'WP_DEBUG_DISPLAY\', false );
    @ini_set( \'display_errors\', \'Off\' );
将此行插入函数示例\\u ajax\\u request函数

function example_ajax_request() {
  if ( isset( $_REQUEST ) ) { $postID = $_REQUEST[\'postID\'];  
 error_log(\'testing ajax\');
 echo $postID; } wp_die(); 
}