Um usuário perguntou ?
Olá,
Sou novo no WP e não estou muito familiarizado com o AJAX, mas estou tentando fazer uma chamada AJAX muito simples funcionar, acho que estou quase lá, mas não consigo obter nenhum resultado para retornar da chamada AJAX , Tudo isso eu reduzi ao mínimo para fazê-lo funcionar e estou apenas retornando a string ‘Estes são resultados de dados AJAX’ da função PHP Ajax, mas mesmo assim não parece estar funcionando. No momento recebo meus alertas de teste…
alert('jquery step 1'); - Works OK
alert('jquery step 2'); - Works OK
alert('ajax result: '.response); - ** NOT DISPLAYED **
alert('jquery step 3'); - Works OK
I've created a custom plugin and custom css, js and php files as follows...
********* c4l-custom-functions.php ********
<?php
/**
* Plugin Name: C4L Custom Functions Plugin
* Description: This plugin contains C4l custom functions, scripts and css styles.
* Author: C4L
* Version: 1.0
*/
function c4l_custom_script_and_style_includer() {
wp_enqueue_script( 'c4l-js', plugins_url( 'js/c4l-custom-scripts.js' , __FILE__ ) );
wp_enqueue_style( 'c4l-css', plugins_url( 'css/c4l-custom-styles.css' , __FILE__ ) );
}
add_action( 'wp_enqueue_scripts', 'c4l_custom_script_and_style_includer' );
add_action( 'wp_ajax_wps_get_time', 'wps_get_time' );
add_action( 'wp_ajax_nopriv_wps_get_time', 'wps_get_time' );
function wps_get_time() {
// $format = $_POST['format'];
echo('This is AJAX data results');
//echo date($format);
die();
}
?>
********* c4l-custom-scripts.js ********
document.addEventListener("DOMContentLoaded", function(event) {
jQuery('#pulldown1').change(function(){
alert('jquery step 1');
var timeformat="U";
alert('jquery step 2');
jQuery.ajax({
type: "POST",
url: "admin-ajax.php",
data: { action: 'wps_get_time', format: timeformat },
success: function ( response ) {
alert('ajax result: '.response);
}
});
alert('jquery step 3');
});
});
(@rossmitchell)
2 anos, 9 meses atrás
Você ativou a depuração? Você examinou os logs do seu servidor web?
(@tugbucket)
2 anos, 9 meses atrás
success: function ( response ) {
alert('ajax result: '.response);
a resposta não é a mesma que a resposta. Tente:
alert('ajax result: ' + response);
(@richardcoffee)
2 anos, 9 meses atrás
Você também deve usar a função wp_die() do wordpress, em vez da função php die().
Isto foi útil?
0 / 0