我有一个带有以下代码的自定义插件
add_action( \'init\', \'my_script_enqueuer\' );
function my_script_enqueuer() {
wp_register_script( "gift_card_redeem", WP_PLUGIN_URL.\'/plugin-folder/gift_card_redeem.js\', array(\'jquery\') );
wp_localize_script( \'gift_card_redeem\', \'myAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' )));
wp_enqueue_script( \'jquery\' );
wp_enqueue_script( \'gift_card_redeem\' );
}
add_action("wp_ajax_gift_card_redeem", "gift_card_redeem");
function gift_card_redeem(){
if(!empty($_SERVER[\'HTTP_X_REQUESTED_WITH\']) && strtolower($_SERVER[\'HTTP_X_REQUESTED_WITH\']) == \'xmlhttprequest\') {
error_log("test !empty");
$result[\'type\'] = "success";
$result = json_encode($result);
echo $result;
}
else {
error_log("test else");
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
die();
}
插件中有一个js文件,名为gift\\u card\\u redeme。js公司
jQuery(document).ready(function () {
jQuery(".redeem_gift_card").click(function (e) {
e.preventDefault();
jQuery.ajax({
type: "post",
dataType: "json",
url: myAjax.ajaxurl,
data: { action: "redeem_gift_card" },
success: function (response) {
if (response.type == "success") {
jQuery("#test").html(response);
} else {
alert("something broke");
}
},
});
});
});
表单签出页面上的php和html如下
<?php
$link = admin_url(\'admin-ajax.php?action=gift_card_redeem\');
echo \'<a class="redeem_gift_card" href="\' . $link . \'" >test</a>\';
?>
<h1 id="test">Test</h1>
我收到一个400错误的管理ajax请求错误。php和我不太清楚为什么。任何帮助都将不胜感激。
谢谢