Un usuario pregunt贸 馃憞
Hola, desde la actualizaci贸n 4.9.4 recibo un error admin-ajax.php 400 al intentar cargar el proyecto.
El ajaxurl se carga correctamente. Aqu铆 est谩n los fragmentos: add_js_scripts () function {wp_enqueue_script (‘script’, get_template_directory_uri (). ‘/Js/ajax-navigation.js’, edit (‘jquery’), ‘1.0’, true);
// pasar la URL de Ajax a script.js wp_localize_script (‘script’, ‘ajaxurl’, admin_url (‘admin-ajax.php’)); } add_action (‘wp_enqueue_scripts’, ‘add_js_scripts’);
$ .ajax ({
url: ajaxurl, beforeSend: function () {
驴Puede usted ayudar? He intentado deshabilitar los plugins, pero est谩s de suerte.
(@jakept)
Hace 2 a帽os, 11 meses
驴Pusiste un gancho en el wp_ajax_
ganchos, y el action
en AJAX data
?
Lanzador de hilos
(@cmdcmdcmd)
Hace 2 a帽os, 11 meses
Hola, no estoy seguro de lo que quieres decir aqu铆 porque estoy cargando contenido a trav茅s de la ID de publicaci贸n y uso la funci贸n .load ()
$ (document) .on (‘touchstart click’, ‘.portfolio_item’, function () {$ port_holder = $ (‘. portfolio_holder’); $ video_holder = $ (‘# video_holder’); $ currentitem = $ (seo) ;
post_id = $ (seo) .attr (‘id-datos’); post_link = $ (seo) .attr (‘data-id’), thetitle = $ (seo) .attr (‘title’), newcontent = post_link + 芦#thevideo禄;
$ .ajax ({
url: ajaxurl, escriba: 芦OBTENER禄, beforeSend: function () {
// anima las miniaturas y agrega una clase ‘actual’
$ port_holder.find (‘. actual’). removeClass (‘actual’); $ currentitem.addClass (‘actual’);
// aseg煤rese de que la parte del contacto ya no sea visible $ j (‘. contactos a’). removeClass (‘activo’); $ j (‘# contactos’). desvanecimiento (300);
$ video_holder.find (‘#el video’). fadeOut (300, function () {this.remove ();});
// ocultar el $ menu (‘. navegaci贸n principal’). fadeOut (300, function () {$ (‘. loader’). display (400);}); $ (‘. portafolio_item’). addClass (‘cerrado’);
}, 茅xito: funci贸n () {
$ (‘. portafolio_item’). addClass (‘abrir’);
$ video_holder.load (newcontent, function () {// $ video_holder.fitVids (); $ (‘. portfolio_item’). removeClass (‘closed’); $ (‘. video_header’). fadeIn (); // mostrar cargador
var theiframe = $ (seo) .find (‘iframe’); theiframe.on (‘load’, function () {$ (‘. loader’). hide (400); $ (this) .addClass (‘visible’);});
// CloseHov ();
$ (‘# el video’). on (‘click touchstart’, function () {$ (‘. portfolio_item’). removeClass (‘open’); $ video_holder.find (‘# thevideo’). fadeOut (300, function () {this.remove () ; $ (‘. navegaci贸n-principal’). fadeIn (300); $ port_holder.find (‘. actual’). removeClass (‘actual’);});
})}); }, total: function () {
}})
})
(@jakept)
Hace 2 a帽os, 11 meses
Est谩 enviando una solicitud AJAX a admin-ajax.php
, pero no est谩 enviando ninguna informaci贸n con la solicitud y no parece haber agregado ning煤n c贸digo para manejar la solicitud. As铆 que no estoy seguro de lo que espera que suceda. Le sugiero que eche otro vistazo a la documentaci贸n: https://codex.wordpress.org/AJAX_in_Plugins
Esta respuesta fue modificada hace 2 a帽os, 11 meses por.
(@bcworkz)
Hace 2 a帽os, 11 meses
Otro recurso de Ajax para WP FWIW: https://developer.wordpress.org/plugins/javascript/ajax/
(@anuragdeshmukh)
Hace 2 a帽os, 11 meses
1. Para usar ajax con WordPress, primero debe usar wp_enqueue_script () para que javascript est茅 incluido en la p谩gina.
wp_enqueue_script( 'ajax-script', plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
2. Luego, debe usar menos de 2 ganchos para agregar funcionalidad y cuando tengamos que despedir a los usuarios que inician sesi贸n y a los que inician sesi贸n.
add_action('wp_ajax_function_name', 'function_name');
add_action('wp_ajax_nopriv_function_name', 'function_name');
3. En su archivo js agregue un c贸digo para llamar a ajax.
jQuery('.class_name').click(function(){
var str = {
'action': 'function_name',
'value1': 'value1 to pass',
'value2': 'value2 to pass',
};
jQuery.ajax({
type: "POST",
dataType: "html",
url: ajax-script.ajaxurl,
data: str,
success: function(data){
// Code after ajax success
},
error : function(jqXHR, textStatus, errorThrown) {
$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
}
});
});
4. Escriba su funci贸n en el archivo functions.php.
function function_name() {
// do stuffs whatever you wish to do in your function
$html = 'ajax';
echo $html; // please make sure echo your output here
wp_die(); // this is required to terminate immediately and return a proper response
}
Espero que esto ayude.
Lanzador de hilos
(@cmdcmdcmd)
Hace 2 a帽os, 11 meses
Hola chicos, he creado un nuevo archivo para ejecutar su proceso, aunque el m铆o estaba funcionando antes, todav铆a obten铆a el error 400.
function add_js_scripts() {
wp_enqueue_script( 'script', get_template_directory_uri() . '/js/ajax-nav.js', array('jquery'), '1.0', true );
// pass Ajax Url to script.js
wp_localize_script('script', 'ajaxurl', admin_url( 'admin-ajax.php' ) );
}
add_action('wp_enqueue_scripts', 'add_js_scripts');
add_action('wp_ajax_function_name', 'AjaxVid');
add_action('wp_ajax_nopriv_function_name', 'AjaxVid');
function AjaxVid(){
$post_id = $_POST['postid'];
$post_title = $_POST['title'];
$link = get_field("video_link", $post_id);
$_video_type = get_field("video_type", $post_id);
$client = get_field("client", $post_id);
$director = get_field("director", $post_id);
echo '<div id="thevideo">';
echo '<div class="video_header">';
echo '<div class="item_title">';
echo $post_title;
echo '</div>';
if(!empty($client)):
echo '<div class="item_client">';
echo $client;
echo '</div>';
endif;
if(!empty($director)):
echo '<span class="sep"> | </span>';
echo '<div class="item_director">';
echo $director;
echo '</div>';
endif ;
echo '</div>';
echo '<div class="video_inner">';
if(!empty($link)){
if($_video_type == "youtube"){
echo '<iframe class="mainvid" width="960" height="540" src="https://www.youtube.com/embed/'. $link .'?enablejsapi=1&iv_load_policy=3&showinfo=0&autoplay=1" frameborder="0" allowfullscreen="1" title="'. $post_title . '"></iframe>';
} elseif ($_video_type == "vimeo"){
echo '<iframe class="mainvid" width="960" height="540" src="https://player.vimeo.com/video/'. $link .'?title=0&wmode=opaque&api=1&autoplay=1" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
} elseif ($_video_type == "other"){
echo '<iframe src="<?php echo $link ;?>" width="960" height="540" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>';
}
}
echo '</div></div>';
}
y js
(function($) {
$(document).on( 'touchstart click', '.portfolio_item', function( ) {
$port_holder = $('.portfolio_holder');
$video_holder = $('#video_holder');
$currentitem = $(this);
post_id = $(this).attr('data-id');
thetitle = $(this).attr('title');
var str = {
'action': 'AjaxVid',
'postid': post_id,
'title': thetitle,
};
$.ajax({
type: "POST",
dataType: "html",
url: ajaxurl,
data: str,
success: function(data){
// Code after ajax success
},
error : function(jqXHR, textStatus, errorThrown) {
$loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);
}
});
});
})(jQuery);
(@anuragdeshmukh)
Hace 2 a帽os, 11 meses
Debe reemplazar wp_ajax_AjaxVid y wp_ajax_nopriv_function_name con wp_ajax_function_name con wp_ajax_nopriv_AjaxVid. Entonces, sus nuevos cambios se ver铆an as铆:
add_action('wp_ajax_AjaxVid', 'AjaxVid');
add_action('wp_ajax_nopriv_AjaxVid', 'AjaxVid');
en la secci贸n js, la URL que necesita ejecutar es:
url: script.ajaxurl,
Lanzador de hilos
(@cmdcmdcmd)
Hace 2 a帽os, 11 meses
Est谩 bien, funcion贸 馃檪 隆隆隆Muchas gracias por tu ayuda !!!
驴Solucion贸 tu problema??
0 / 0