Ajax 来自前端的调用不适用于 wordpress
Ajax call from frontend not working with wordpress
调用 Ajax 无法从 WordPress 的前端工作,我只想根据 id 从数据库中获取数据,这就是我到目前为止所尝试的方法。
//Frontend view
<a href="javascript:void(0)" onclick="getDetails(<?=$index;?>)" >
<?php echo $value ?>
</a>
//Functions.php
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/assets/js/ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
add_action('wp_ajax_nopriv_more_posts', 'get_more_posts');
function get_more_posts(){
// How to get id here to query for the database
echo "Hello World";
exit();
}
// Ajax scripts File
function getDetails(id)
{
jQuery.ajax({
url: more_posts.ajax_url,
type: "GET",
data: {
action: 'more_posts'
},
dataType: "html",
success: function(response){
alert(response);
}
});
}
我遇到了这个错误
reference error more_posts is not defined
//Frontend view
<a href="javascript:void(0)" onclick="getDetails(<?=$index;?>)" >
<?php echo $value ?>
</a>
//Functions.php
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/assets/js/ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
add_action('wp_ajax_nopriv_more_posts', 'get_more_posts');
function get_more_posts(){
// How to get id here to query for the database
echo "Hello World";
echo $_GET['id'];
exit();
}
// Ajax scripts File
function getDetails(id)
{
var id = id;
jQuery.ajax({
url: url: my_ajax_object.ajax_url,
type: "GET",
data: {
action: 'get_more_posts',
id: id,
},
dataType: "html",
success: function(response){
alert(response);
}
});
}
这应该有效!您不需要带有“href
”的绝对路径或 var,url: my_ajax_object.ajax_url
就足够了,因为您已经使用 wp_enqueue_script
添加了 admin-ajax.php。谢谢
你错了
url: more_posts.ajax_url
因为你本地化的脚本名称是my_ajax_object
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
试试这个
url: my_ajax_object.ajax_url
调用 Ajax 无法从 WordPress 的前端工作,我只想根据 id 从数据库中获取数据,这就是我到目前为止所尝试的方法。
//Frontend view
<a href="javascript:void(0)" onclick="getDetails(<?=$index;?>)" >
<?php echo $value ?>
</a>
//Functions.php
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/assets/js/ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
add_action('wp_ajax_nopriv_more_posts', 'get_more_posts');
function get_more_posts(){
// How to get id here to query for the database
echo "Hello World";
exit();
}
// Ajax scripts File
function getDetails(id)
{
jQuery.ajax({
url: more_posts.ajax_url,
type: "GET",
data: {
action: 'more_posts'
},
dataType: "html",
success: function(response){
alert(response);
}
});
}
我遇到了这个错误
reference error more_posts is not defined
//Frontend view
<a href="javascript:void(0)" onclick="getDetails(<?=$index;?>)" >
<?php echo $value ?>
</a>
//Functions.php
function my_enqueue() {
wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/assets/js/ajax-script.js', array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
add_action('wp_ajax_nopriv_more_posts', 'get_more_posts');
function get_more_posts(){
// How to get id here to query for the database
echo "Hello World";
echo $_GET['id'];
exit();
}
// Ajax scripts File
function getDetails(id)
{
var id = id;
jQuery.ajax({
url: url: my_ajax_object.ajax_url,
type: "GET",
data: {
action: 'get_more_posts',
id: id,
},
dataType: "html",
success: function(response){
alert(response);
}
});
}
这应该有效!您不需要带有“href
”的绝对路径或 var,url: my_ajax_object.ajax_url
就足够了,因为您已经使用 wp_enqueue_script
添加了 admin-ajax.php。谢谢
你错了
url: more_posts.ajax_url
因为你本地化的脚本名称是my_ajax_object
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
试试这个
url: my_ajax_object.ajax_url