admin管理员组文章数量:1429524
I have a simple ajax script to send a form in WordPress.
My PHP code to get the ajax URL is:
wp_localize_script( 'custom_child', 'jAjax', array('ajax_url'
=>admin_url('admin-ajax.php')));
Moreover I have in functions.php the following:
function enviar_pedido_clientes() {
// do something with the data to send an email
wp_mail( $to, $subject, $message, $headers );
die();
}
add_action( 'admin_ajax_nopriv_pedido_clientes', 'enviar_pedido_clientes' );
add_action( 'admin_ajax_pedido_clientes', 'enviar_pedido_clientes' );
Finally, in my .js:
$('#enviar_fotos').on('click', function(event) {
event.preventDefault();
/* Act on the event */
data = {
'action': 'pedido_clientes',
'nombreCliente': 'pedro', // that's how we get params from wp_localize_script() function
'emailCliente' : '[email protected]'
};
$.ajax({
url: jAjax.ajax_url, // URL to "wp-admin/admin-ajax.php"
data: data,
method: "POST", // use $_POST request to submit data
// success:function( data ) {
// $( '#probando' ).html( data );
// },
error: function(){
console.log('Ha habido un error'); // error
}
});
});
I went through many posts to try to find the error, but I cannot find it.
I have a simple ajax script to send a form in WordPress.
My PHP code to get the ajax URL is:
wp_localize_script( 'custom_child', 'jAjax', array('ajax_url'
=>admin_url('admin-ajax.php')));
Moreover I have in functions.php the following:
function enviar_pedido_clientes() {
// do something with the data to send an email
wp_mail( $to, $subject, $message, $headers );
die();
}
add_action( 'admin_ajax_nopriv_pedido_clientes', 'enviar_pedido_clientes' );
add_action( 'admin_ajax_pedido_clientes', 'enviar_pedido_clientes' );
Finally, in my .js:
$('#enviar_fotos').on('click', function(event) {
event.preventDefault();
/* Act on the event */
data = {
'action': 'pedido_clientes',
'nombreCliente': 'pedro', // that's how we get params from wp_localize_script() function
'emailCliente' : '[email protected]'
};
$.ajax({
url: jAjax.ajax_url, // URL to "wp-admin/admin-ajax.php"
data: data,
method: "POST", // use $_POST request to submit data
// success:function( data ) {
// $( '#probando' ).html( data );
// },
error: function(){
console.log('Ha habido un error'); // error
}
});
});
I went through many posts to try to find the error, but I cannot find it.
Share Improve this question asked May 2, 2019 at 16:35 Kevin MamaqiKevin Mamaqi 3881 gold badge8 silver badges21 bronze badges1 Answer
Reset to default 2The hook prefix is wrong — it should be wp_
and not admin_
:
add_action( 'admin_ajax_nopriv_pedido_clientes', 'enviar_pedido_clientes' );
add_action( 'admin_ajax_pedido_clientes', 'enviar_pedido_clientes' );
So the correct code is:
add_action( 'wp_ajax_nopriv_pedido_clientes', 'enviar_pedido_clientes' );
add_action( 'wp_ajax_pedido_clientes', 'enviar_pedido_clientes' );
本文标签: Error 400 bad request using adminajaxphp
版权声明:本文标题:Error 400 bad request using admin-ajax.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745531028a2662060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论