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
解决方案
尝试在您的 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' );
推荐阅读
- r - 在 r 中使用 gganimate transition_reveal 获取空白动画时间序列图
- jenkins - 詹金斯 + Kubernetes + ccache
- java - 汤姆猫的春季启动问题(无法启动组件 [StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]])
- apache-spark - 在 csv vs delta 表中使用 df.coalesce(1)
- java - java编译器错误地将字段引用与包名称?
- azure-data-factory - ADF:将不同的值复制到查找表中,将 FK 列添加到数据集
- c - C 包括:last_component 在哪里定义?
- angular - 可以在具有功能的模态中导入模块html标签吗?
- python - matplotlib 轴值未排序
- snowflake-cloud-data-platform - Snowflake 如何处理对象标识符中的区分大小写?