最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Error 400 bad request using admin-ajax.php

programmeradmin0浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

The 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' );
发布评论

评论列表(0)

  1. 暂无评论