【问题标题】:jQuery.post returns 0jQuery.post 返回 0
【发布时间】:2021-03-21 05:14:40
【问题描述】:

我目前正在开发一个作为插件创建的 WordPress 登录弹出窗口。特别是我对 jQuery 和 AJAX 比较陌生。我有一个返回值始终为 0 的小问题,尽管我认为这实际上是成功消息并且它没有返回任何内容。

任何帮助将不胜感激。

1. Ajaxurl 定义

wp_localize_script(
    'mytheme', 'Params', apply_filters( 'js_params', array(
        'siteUrl' => esc_url( site_url( '/' ) ),
        'homeUrl' => esc_url( home_url( '/' ) ),
        'ajaxUrl' => esc_url( admin_url( 'admin-ajax.php' ) ),
        ),
    ) )
);

2。功能登录表单

function login_form( $atts = array() ) {

    $output = '';

    wp_enqueue_script( 'jquery-ui-tabs', true );
    wp_enqueue_script( 'loginform' );

    $class .= " login-form login-form-container";

    $output .= '<div class="' . $class . '"';

    $output .= '>';
    
    $output .= '<div class="login-form-inner">';

    /* Title */
    $output .= '<div class="login-form-title-container">';
    $output .= '<h3 class="login-form-title">';
    $output .= esc_html__( 'Login' );
    $output .= '</h3>';
    $output .= '</div>';

    ob_start();

    ?>
    <form class="login-form" action="<?php echo esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) ); ?>" method="post">
            <?php do_action( 'woocommerce_login_form_start' ); ?>

            <p class="login-usernam">
                <label for="username"><?php esc_html_e( 'Username or email address'); ?>&nbsp;<span class="required">*</span></label>
                <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="username" id="username" autocomplete="username" value="<?php echo ( ! empty( $_POST['username'] ) ) ? esc_attr( wp_unslash( $_POST['username'] ) ) : ''; ?>" /><?php // @codingStandardsIgnoreLine ?>
            </p>
            <p class="login-password">
                <label for="password"><?php esc_html_e( 'Password', ); ?>&nbsp;<span class="required">*</span></label>
                <input class="woocommerce-Input woocommerce-Input--text input-text" type="password" name="password" id="password" autocomplete="current-password" />
            </p>

            <?php do_action( 'woocommerce_login_form' ); ?>

            <p class="login-submit">
                <?php wp_nonce_field( 'woocommerce-login', 'woocommerce-login-nonce' ); ?>
                <input id="wp-submit" type="submit" class="<?php echo apply_filters( 'wvc_login_form_submit_button_class', 'button button-primary' ); ?>" name="login" value="<?php esc_attr_e( 'Log in'); ?>">
            </p>

            <?php do_action( 'woocommerce_login_form_end' ); ?>
        </form>
    <?php

    $output .= ob_get_clean();

    $output .= '</div><!--.login-form-inner-->';

    $output .= '</div><!--.login-form-->';

    return $output;
}

3.函数回调

function get_login_form() {

    if ( function_exists( 'login_form' ) ) {
        echo login_form();
    }
    exit;
}

add_action( 'wp_ajax_get_login_form', 'get_login_form' );
add_action( 'wp_ajax_nopriv_get_login_form', 'get_login_form' );

4. jQuery

        loginPopup : function() {

            var $body = $( 'body' );

            $( document ).on( 'click', '.account-item-icon-user-not-logged-in, .close-loginform-button', function( event ) {
                event.preventDefault();

                if ( $body.hasClass( 'loginform-popup-toggle' ) ) {

                    $body.removeClass( 'loginform-popup-toggle' );

                } else {
                    
                    $body.removeClass( 'overlay-menu-toggle' );

                    if ( $( '.wvc-login-form' ).length ) {

                        $body.addClass( 'loginform-popup-toggle' );

                    } else {
                        /* AJAX call */
                        $.post( Params.ajaxUrl, { action : 'get_login_form' }, function( response ) {
                            
                            console.log( response );

                            if ( response ) {

                                $( '#loginform-overlay-content' ).append( response );

                                $body.addClass( 'loginform-popup-toggle' );
                            }
                        } );
                    }
                }
            } );

【问题讨论】:

    标签: jquery ajax wordpress authentication


    【解决方案1】:

    函数 get_login_form() 返回“exit;”。那应该是 wp_die(),这就是为什么你得到一个返回值 0。

    任何用 ajax 调用的 php 函数都必须回显结果并以 wp_die() 结束。

    【讨论】:

    • 我试过wp_die()但还是不行
    • 除了缺少必需的 wp_die() 之外,您的代码仍然有很多错误,您使用 ob_get_clean() 但之后添加到变量中。 get_login_form 必须在回显任何数据以发送到 ajax 脚本后返回 wp_die()。您还尝试将脚本排入函数队列,但这完全是另一个问题。它们应该在 wp_enqueue_scripts 中。
    • 感谢您提供的信息。你能帮我解决这些问题吗?我又试了一次还是不行
    • 由于您的代码中有这么多问题,我认为尝试重写它是不明智的,因为我认为您应该在尝试之前更熟悉 php、wordpress 和 woocommerce重做一些像登录表单这样严肃的事情。你正在向前方许多许多问题敞开大门。
    • 我认为最好的办法是从 login_form 函数中删除表单代码,并将其替换为“echo hi;wp_die();”看看这是否有效。
    猜你喜欢
    • 2012-04-11
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    相关资源
    最近更新 更多