Php Wordpress Ajax请求中出现400个错误请求
Php Wordpress Ajax请求中出现400个错误请求,php,ajax,wordpress,wordpress-theming,Php,Ajax,Wordpress,Wordpress Theming,在我的单个帖子页面中,我创建了下载按钮,如果有人点击按钮,ajax将运行,我的php函数也将运行,我在WordPress中发送ajax请求,但它返回404错误请求。 以下是屏幕截图: 代码如下: $('body').on('click', 'a.template_download_button', function(){ var post_id = $(this).attr('data-postid'); console.log(post_id); console.l
在我的单个帖子页面中,我创建了下载按钮,如果有人点击按钮,ajax将运行,我的php函数也将运行,我在WordPress中发送ajax请求,但它返回
404错误请求。
以下是屏幕截图:
代码如下:
$('body').on('click', 'a.template_download_button', function(){
var post_id = $(this).attr('data-postid');
console.log(post_id);
console.log(customJSAjax);
console.log(customJSAjax.ajaxurl);
$.ajax({
type : "post",
url : customJSAjax.ajaxurl,
data : {action: "template_downloads", post_id : post_id},
success: function(response) {
console.log(response);
}
});
});
php:
<?php
function inject_scripts() {
wp_enqueue_script( 'inject-custom-js', get_template_directory_uri() . '/js/custom.js', array(), '20151215', true );
wp_localize_script( 'inject-custom-js', 'customJSAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
}
add_action( 'wp_enqueue_scripts', 'inject_scripts' );
function template_downloads(){
$post_id = $_REQUEST['post_id'];
var_dump($post_id);
die();
}
add_action("wp_ajax_template_downloads", "template_downloads");
可能需要添加add_操作('wp_ajax_nopriv_template_downloads'、'template_downloads');伙计,谢谢你可能需要添加add_操作('wp_ajax_nopriv_template_downloads','template_downloads');谢谢你,伙计