我在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>
很遗憾,我收到了错误:为什么Ajax代码在这里不起作用?是否需要连接其他文件?