Javascript Can';我不能让WordPressAjax工作
我一直在关注这方面的多个教程(包括Wordpress自己的),这让我有点疯狂 我正试图让AJAX在Wordpress的前端工作,但它只是不做任何事情,我知道该函数可以工作,因为我已经独立测试了它,您的输入将非常感谢。非常感谢 functions.php中的代码Javascript Can';我不能让WordPressAjax工作,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我一直在关注这方面的多个教程(包括Wordpress自己的),这让我有点疯狂 我正试图让AJAX在Wordpress的前端工作,但它只是不做任何事情,我知道该函数可以工作,因为我已经独立测试了它,您的输入将非常感谢。非常感谢 functions.php中的代码 function call_ajax() { //assuming this is in a theme? wp_enqueue_script( 'ajax_car_models', get_template_di
function call_ajax()
{
//assuming this is in a theme?
wp_enqueue_script( 'ajax_car_models', get_template_directory_uri() . '/js/ajax_car_models.js', array( 'jquery' ), null, true);
//loacalize the script using the same handle it was registered / enqueued with
wp_localize_script( 'ajax_car_models', 'my_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
//make a call to call_ajax at the right time
add_action( 'wp_enqueue_scripts', 'call_ajax' );
add_action( 'wp_ajax_ajax_car_models', 'ajax_car_models' );
add_action( 'wp_ajax_nopriv_ajax_car_models', 'ajax_car_models' );
function ajax_car_models() {
$args = array( 'post_type' => 'product', 'posts_per_page' => 1040 );
$loop1 = new WP_Query( $args );
$code = '';
$car_make = 26;
$all_models = array();
while ( $loop1->have_posts() ) : $loop1->the_post();
$terms_make = get_the_terms( get_the_id(), 'pa_car-make' );
foreach ( $terms_make as $make ) {
if ( $make->term_id == $car_make ) {
$the_car_model = get_the_terms( get_the_id(), 'pa_model' );
foreach ( $the_car_model as $the_model ) {
if ($all_models[$the_model->name] != 'true') {
$code .= '<br />'.$the_model->name.' ';
$all_models[$the_model->name] = 'true';
}
}
}
}
endwhile;
wp_reset_postdata();
header( 'Content-type: application/json' );
echo json_encode( $code );
die();
}
根据PHP手册,需要对字符串数据进行UTF8编码 或者,您可以尝试在functions.php中执行此操作
$return_array = array(
'val' => $code
);
echo json_encode($return_array);
然后,在jQuery脚本中
jQuery("#feedback").html(data.val);
另外,尝试在ajax请求中添加一个选项
url: my_ajax_script.ajaxurl,
dataType: 'json', // add this line
data: ({action : 'ajax_car_models'}),
希望这些帮助 如果您将javascript内联,您将无法使用
本地化
将ajax url传递给它
您应该将javascript移动到另一个文件中,然后使用wp\u enqueue\u script
,以便正确使用ajax
function call_ajax()
{
//assuming this is in a theme?
wp_enqueue_script( 'ajax_car_models', get_template_directory_uri() . '/js/ajax_car_models.js', array( 'jquery' ), null, true);
//loacalize the script using the same handle it was registered / enqueued with
wp_localize_script( 'ajax_car_models', 'my_ajax_script', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
//make a call to call_ajax at the right time
add_action( 'wp_enqueue_scripts', 'call_ajax' );
您还需要在响应函数中发送json头
header( 'Content-type: application/json' );
echo json_encode( $code );
exit();
在ajax调用中,您没有将数据传递给成功回调
success: function(data){
console.log(data);
//...
}
您可以使用chrome开发者工具上的“网络”选项卡查看实际发出的请求,并检查它们收到的响应 “my_ajax_script.ajaxurl”设置在哪里?这在wp_localize_script函数中,是否不正确?在哪里。。。在javascript中?这在JS中是不可访问的。。。它们是分开的。。PHP服务器端JS front sidelagbox不正确,
localize
是传递ajax url的标准方式,但看起来您缺少了一些东西。请看我的答案。谢谢你的输入,非常感谢,我已经实现了你所说的,并更新了上面的代码,JS现在在一个单独的文件中。Chrome中的“网络”选项卡显示响应,但仍然没有显示任何内容。我尝试在函数中执行一个简单的回显,甚至尝试了一个成功警报,但仍然没有。我发现您没有将数据传递给成功回调,请参阅我的编辑。你说的成功警报是指成功回调中的警报()
?如果进入“网络”选项卡,您可以看到返回的响应代码,应该是200,如果单击它,您可以看到发送的标题和收到的响应。非常感谢您的帮助,它现在可以工作了。我是AJAX新手,一般来说对jQuery也相当陌生,我们都必须从某个地方开始!再次感谢您的投入。
success: function(data){
console.log(data);
//...
}