Wordpress Ajax strange print

63
December 21, 2018, at 3:40 PM

I have an ajax issue on Wordpress. I have a simple Select with a button wich have an onclick function who work with ajax. That function, who call a file named ajax-filter-system.php that have a simple string "HELLO WORLD", reload the div content with class 'ajax-loader', print 'HELLO WORD0' with '0' at the end of string. Can't understand why... There is codes:

Function.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>

Function onclick (js file)

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);
            }
        }
    });
});
}

If i change

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

in

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

it's work fine

Thanks Alot!

Answer 1

You missed exit in end of the function.

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;
}
READ ALSO
How to retreive the lenght of an array? [on hold]

How to retreive the lenght of an array? [on hold]

i try to setup a carousel for a web project i use javascript my code seems to works but i its seem that i can retreive the result of variable or object with the syntax "this"

34
How can I order numerical columns with jQuery &ldquo;sortElements&rdquo; plugin?

How can I order numerical columns with jQuery “sortElements” plugin?

I'm using the jQuery script "sortElements" on my tables to sort their columns, but I noticed that the sorting in numerical columns doesn't workThe result of the sorting on them is something like "100 20 250 30"

45
HTML-Angular 6 Stop parent DOM element event from being fired by one of the child elements

HTML-Angular 6 Stop parent DOM element event from being fired by one of the child elements

There were a couple of similar threads - but they are all related to event propagation, but my requirement is somewhat differentHere's my HTML:

25
How to create Biker&#39;s Optimize Route for delivering Packages using Nodejs

How to create Biker's Optimize Route for delivering Packages using Nodejs

A Company must deliver the number of packages in City

23