WordPress admin-ajax.php错误请求400

时间:2019-04-30 15:32:51

标签: php jquery wordpress

您好,我正在保存以供以后在我的网站中使用 但是当我单击按钮

时,我收到了管理员ajax错误的请求

functions.php

$ awk '/A.2|A.5|B.556/ { printf("%s%s%s%s",prev,FS,$2,RS); } { prev=$2; }' file.txt
1 2
2 5
23 556

save-for-later.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.js

  <?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
}

我不知道我在做什么错 每当我单击添加以保存以供以后使用按钮时,我都会收到错误admin-ajax.php 400

2 个答案:

答案 0 :(得分:2)

尝试在您的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' ),
            'user'.           => get_current_user_id(),
            'post_id'         => get_the_ID(),
            'user_product'    => get_the_ID(), // custom function if any
            'response'        => your_custom_function(), // if any
          );

          if ( is_page( 'savelist' ) ) {
             wp_enqueue_script('remove-prodduct-from-list');
          }
}

add_action( 'wp_enqueue_scripts', 'zumra_scripts' );

答案 1 :(得分:0)

解决了一个名为WPAML的插件导致此冲突的问题 谢谢大家