【问题标题】:Wordpress Ajax strange printWordpress Ajax 奇怪的打印
【发布时间】:2019-05-21 20:05:26
【问题描述】:

我在 Wordpress 上有一个 ajax 问题。 我有一个简单的 Select 按钮,它有一个使用 ajax 的 onclick 功能。 该函数调用一个名为 ajax-filter-system.php 的文件,该文件具有一个简单的字符串“HELLO WORLD”,使用类“ajax-loader”重新加载 div 内容,在末尾打印“HELLO WORD0”和“0”细绳。无法理解为什么... 有代码:

函数.php

add_action('wp_ajax_load_specialita', 'load_specialita_ajax_handler');
add_action('wp_ajax_nopriv_load_specialita', 'load_specialita_ajax_handler');
function load_specialita_ajax_handler() {
    require(get_stylesheet_directory().'/assets/ajax/ajax-filter-system.php'); 
}

ajax-filter-system.php

<div>Hello World</div>

函数onclick(js文件)

function showSpecialita() {
jQuery(function($) {
    var select_target = "";
    var select_target = $('#specialita-selected').val(); // <select> ID

    var xhttp;
    $.ajax({
        url : 'https://www.mysite.it/wp-admin/admin-ajax.php', // AJAX handler
        data : { action : 'load_specialita', specialita : select_target },
        type : 'POST',
        success : function( result ){
            if( result ) {
                $('.ajax-loader-specialita').html(result);
            }
        }
    });
});
}

如果我改变了

$('.ajax-loader-specialita').html(result);  

$('.ajax-loader-specialita').html('Hello Word');

一切正常

非常感谢!

【问题讨论】:

    标签: javascript ajax wordpress


    【解决方案1】:

    你在函数末尾错过了exit

    add_action('wp_ajax_load_specialita', 'load_specialita_ajax_handler');
    add_action('wp_ajax_nopriv_load_specialita', 'load_specialita_ajax_handler');
    function load_specialita_ajax_handler() {
        require(get_stylesheet_directory().'/assets/ajax/ajax-filter-system.php'); 
        exit;
    }
    

    【讨论】:

    • 我会,但我可以在 9 分钟内完成
    猜你喜欢
    • 2020-05-25
    • 2018-08-08
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2014-06-10
    相关资源
    最近更新 更多