简体   繁体   English

Wordpress admin-ajax.php错误400错误请求

[英]Wordpress admin-ajax.php error 400 bad request

hello im doing save for later feature in my website but i get admin-ajax bad request when i click the button 你好,我正在为我的网站中的以后功能做保存,但是当我单击按钮时,我收到了管理员-ajax错误的请求

functions.php 的functions.php

function zumra_scripts() { 
    wp_register_script( 'remove-prodduct-from-list', get_template_directory_uri() . '/js/remove-product.js', array('jquery'), false, true );
    wp_localize_script( 'remove-prodduct-from-list', 'sfl_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )) );
    if ( is_page('savelist') ) {
        wp_enqueue_script( 'remove-prodduct-from-list' );
    }

}
add_action( 'wp_enqueue_scripts', 'zumra_scripts' );

save-for-later.php 节省换later.php

  <?php

add_action( 'wp_ajax_my_action', 'my_action' );

function my_action() {
    $user =         $_POST['user'];
    $post_id =      $_POST['post_id'];
    $response =     $_POST['saveForLater'];
    $response .= '<a href="'. site_url("/savelist") .'">'. __( 'Browse Savelist', 'zumra' ) .'</a>';

    add_user_meta( $user, 'product_id', $post_id);

    echo $response;

    wp_die(); // this is required to terminate immediately and return a proper response
}

add_action( 'wp_ajax_remove_product_from_list', 'remove_product_from_list' );
function remove_product_from_list() {
    $user =     intval( $_POST['user'] );
    $product =  intval( $_POST['product'] );

    delete_user_meta( $user, 'product_id', $product);

    wp_die(); // this is required to terminate immediately and return a proper response
}

add_action( 'wp_ajax_move_to_cart', 'move_to_cart' );
function move_to_cart() {
    $user =     intval( $_POST['user'] );
    $product =  intval( $_POST['product'] );

    delete_user_meta( $user, 'product_id', $product);

    // do_action( 'woocommerce_ajax_added_to_cart', $product );
    // wc_add_to_cart_message( $product );

    wp_die(); // this is required to terminate immediately and return a proper response
}

save-for-later.js 节省换later.js

    jQuery(document).ready(function($) {
    $('.ajax-form').on('submit',function(e){
        e.preventDefault();
        var data = {
            'action': 'my_action',
            'saveForLater': sfl_ajax.response,
            'user':         sfl_ajax.user,
            'post_id':      sfl_ajax.post_id,
            'product_id':   sfl_ajax.user_product,
        };

        // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
        jQuery.post(sfl_ajax.ajaxurl, data, function(response) {
            $('.save-for-later').html(response);
        });
    });
});

i don't know what im doing wrong i get error admin-ajax.php 400 every time i click add to save for later button 我不知道我在做什么错,每次我单击“添加”以保存以后按钮时,都会收到错误admin-ajax.php 400

Try with below code. 尝试下面的代码。 I am assuming that you have include this file in the functions.php of your active theme. 我假设您已经将此文件包含在活动主题的functions.php中。

Or 要么

You can add this code in your functionctions.php directly. 您可以直接在functionctions.php中添加此代码。

As is described in the Wordpress AJAX documentation, you have two different hooks - wp_ajax_(action), and wp_ajax_nopriv_(action). 如Wordpress AJAX文档中所述,您有两个不同的钩子-wp_ajax_(action)和wp_ajax_nopriv_(action)。 The difference between these is: 这些之间的区别是:

  • wp_ajax_(action): This is fired if the ajax call is made from inside the admin panel. wp_ajax_(action):如果从管理面板内部进行ajax调用,则将触发此事件。
  • wp_ajax_nopriv_(action): This is fired if the ajax call is made from the front end of the website. wp_ajax_nopriv_(action):如果从网站的前端进行ajax调用,则将触发此事件。

     add_action( 'wp_ajax_my_action', 'my_action' ); add_action("wp_ajax_nopriv_my_action","remove_product_from_list"); function my_action() { $user = $_POST['user']; $post_id = $_POST['post_id']; $response = $_POST['saveForLater']; $response .= '<a href="'. site_url("/savelist") .'">'. __( 'Browse Savelist', 'zumra' ) .'</a>'; add_user_meta( $user, 'product_id', $post_id); echo $response; wp_die(); // this is required to terminate immediately and return a proper response } add_action( 'wp_ajax_remove_product_from_list', 'remove_product_from_list' ); add_action("wp_ajax_nopriv_remove_product_from_list","remove_product_from_list"); function remove_product_from_list() { $user = intval( $_POST['user'] ); $product = intval( $_POST['product'] ); delete_user_meta( $user, 'product_id', $product); wp_die(); // this is required to terminate immediately and return a proper response } add_action( 'wp_ajax_move_to_cart', 'move_to_cart' ); add_action("wp_ajax_nopriv_move_to_cart","GetEditForm"); function move_to_cart() { $user = intval( $_POST['user'] ); $product = intval( $_POST['product'] ); delete_user_meta( $user, 'product_id', $product); // do_action( 'woocommerce_ajax_added_to_cart', $product ); // wc_add_to_cart_message( $product ); wp_die(); // this is required to terminate immediately and return a proper response } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM