php - Wordpress admin-ajax.php 错误 400 错误请求
问题描述
您好,我正在为我的网站中的以后功能保存,但是当我单击按钮时,我收到了 admin-ajax 错误请求
函数.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' );
稍后保存.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
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);
});
});
});
我不知道我做错了什么,每次单击添加以保存以备后用按钮时,我都会收到错误 admin-ajax.php 400
解决方案
试试下面的代码。我假设您已将此文件包含在活动主题的 functions.php 中。
或者
您可以直接在您的functionctions.php 中添加此代码。
如 Wordpress AJAX 文档中所述,您有两个不同的挂钩 - wp_ajax_(action) 和 wp_ajax_nopriv_(action)。这些之间的区别是:
- wp_ajax_(action):如果从管理面板内部进行 ajax 调用,则会触发此事件。
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 }
推荐阅读
- flutter - 如何集成krpano在flutter App中展示全景图
- android - 在Android Studio中杀死应用程序后如何运行handler.postDelayed?
- c# - 几秒钟后如何自动运行一次性任务
- bash - Bash 脚本 - 检查公共 IP 更改的次数
- python - Python to Exe“无法执行exe脚本”错误
- python - 将文本文件中的列表转换为 Python 上的字典
- html - CSS 定位:内联图像对齐问题
- nginx - 上游在发送到客户端时过早关闭连接
- javascript - 出现意外结果的循环
- bash - gnome-terminal -- $SHELL 无法执行最简单的命令