使用admin-ajax.php发出的AJAX POST请求的状态400

时间:2020-11-27 作者:JAGENI Nat

从昨天开始,我就一直在处理这个问题,编写并重写代码来修复这个400 XRH错误。我找不到办法来修理它。

在向您展示代码之前,下面是一些上下文:

我正在尝试将可变产品添加到归档循环中。我已经成功地将变体选择表单带到了那里,我可以使用AJAX发送正确的数据。问题似乎与请求有关。

这是我的代码:

// The main php function

function variation_add_to_cart_product(){

    $product_id = apply_filters(\'woocommerce_add_to_cart_product_id\', absint($_POST[\'product_id\']));
    $quantity = empty($_POST[\'quantity\']) ? 1 : wc_stock_amount($_POST[\'quantity\']);
    $variation_id = absint($_POST[\'variation_id\']);
    $passed_validation = apply_filters(\'woocommerce_add_to_cart_validation\', true, $product_id, $quantity);
    $product_status = get_post_status($product_id);

    if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id) && \'publish\' === $product_status) {

            do_action(\'woocommerce_ajax_added_to_cart\', $product_id);

            if (\'yes\' === get_option(\'woocommerce_cart_redirect_after_add\')) {
                    wc_add_to_cart_message(array($product_id => $quantity), true);
            }

            WC_AJAX :: get_refreshed_fragments();
    } else {

            $data = array(
                    \'error\' => true,
                    \'product_url\' => apply_filters(\'woocommerce_cart_redirect_after_error\', get_permalink($product_id), $product_id));

            echo wp_send_json($data);
    }

    wp_die();

}
// Its actions

add_action( \'wp_ajax_variation_add_to_cart_product\', \'variation_add_to_cart_product\' );
add_action( \'wp_ajax_nopriv_variation_add_to_cart_product\', \'variation_add_to_cart_product\' );

// The loading of the javascript file and the ajax url

add_action( \'wp_enqueue_scripts\', \'vl_load_js_script\' );

function vl_load_js_script() {
    wp_register_script(\'vl_script\', plugins_url( \'js/vl-js.js\', __FILE__ ));
    wp_enqueue_script(\'vl_script\');

    $vl_ajax_array = array(
        \'ajax_url\' => admin_url( \'admin-ajax.php\' )
    );

    // pass Ajax Url to script.js
    wp_localize_script(\'vl_script\', \'vl_script\', $vl_ajax_array );
}
// The jQuery AJAX function

jQuery(function ($) {

  // Targeting the variation add_to_cart button
  $(document).on(\'click\', \'.vl_add_to_cart_button\', function (e) {
    e.preventDefault();

    // Selecting the clicked button
    var $thisbutton = $(this),
      // Selecting the variation form
      $form = $thisbutton.parents(\'.woo-entry-inner\').find(\'.woocommerce-variation-add-to-cart-enabled\'),
      // Selecting the variation\'s product id
      product_id = $form.find(\'input[name=product_id]\').val(),
      // Selecting the variation\'s id
      variation_id = $form.find(\'input[name=variation_id]\').val(),
      // Selecting the variation\'s quantity
      variation_qty = $form.find(\'input[name=variation_qty]\').val();

    // Creating the data array to send the data
    var data = {
      action: \'vl_ajax_add_to_cart\',
      product_id: product_id,
      product_sku: \'\',
      quantity: variation_qty,
      variation_id: variation_id,
    };

    $(document.body).trigger(\'adding_to_cart\', [$thisbutton, data]);

    $.ajax({
      type: \'post\',
      url: vl_script.ajax_url,
      data: data,
      beforeSend: function (response) {
        $thisbutton.removeClass(\'added\').addClass(\'loading\');
      },
      complete: function (response) {
        $thisbutton.addClass(\'added\').removeClass(\'loading\');
      },
      success: function (response) {

        if (response.error && response.product_url) {
          window.location = response.product_url;
          return;
        } else {
          $(document.body).trigger(\'added_to_cart\', [response.fragments, response.cart_hash, $thisbutton]);
        }
      },
    });

    return false;
  });

});
发送的请求有效负载为action=vl_ajax_add_to_cart&product_id=3468&product_sku=&quantity=5&variation_id=5249.

答案是0.

ajax地址为localhost/biotruck/wp-admin/admin-ajax.php.

1 个回复
最合适的回答,由SO网友:Tom J Nowell 整理而成

您声明:

wp_ajax_variation_add_to_cart_product
但后来用了这个:

action: \'vl_ajax_add_to_cart\',
揭示问题:

variation_add_to_cart_product != vl_ajax_add_to_cart

这两个必须匹配,但它们不匹配。

相关推荐

为什么我的函数.php文件在主题中会自动删除?

我用的是电子主题。这很好,我在函数中添加了一些woocommerce订单取消代码。主题的php文件。之后,我的网站显示了一个错误,即;您的网站出现严重错误"E;。当我检查我的Cpanel时。。。我发现这个函数。我的主题的php文件丢失。我在本地主机上有这个网站,一切都很顺利。我上传了函数。从我的localhost主题到Cpanel的php文件,我的站点运行得很好,但只需7-10秒。。之后,它显示了相同的错误,当我检查Cpanel时,我发现functiono。php再次丢失。。我又上传了一次,但又被