我尝试,当单击submit按钮时,用一些值传递ajax请求,然后计算这些值并返回。
My Problem : 当我进行自定义Ajax调用时,我从管理Ajax得到400个错误。php。请帮我找出我的错误或最好的方法来做这件事。
Ajax Call - footer.php(theme footer)
<script>
$(function() {
$("#calc_form").on("submit", function(e) {
// alert("submit");
e.preventDefault();
$.ajax({
method: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: \'calculation\',
message_id: $(\'#tot_tax_three_month_Input\').val()
},
dataType: \'json\',
success: function (output) {
}
});
});
});
</script>
Admin-ajax.php
// handle the ajax request
function calculation() {
$value = $_REQUEST[\'message_id\'];
echo json_encode(array("value" => $value));
die();
}
// register the ajax action for authenticated users
add_action(\'wp_ajax_calculation\', \'calculation\');
// register the ajax action for unauthenticated users
add_action(\'wp_ajax_nopriv_calculation\', \'calculation\');
test.php
<form id="calc_form">
<table class="table">
<thead>
<tr>
<th style="width: 40%" scope="col"></th>
<th scope="col">Per month LKR</th>
<th scope="col">For 3 months period* LKR</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2" class=" text-danger"><b>Tax amount for 3 months</b></td>
<td><input type="text" name="" id="tot_tax_three_month_Input" class="form-control " /></td>
</tr>
<tr>
<td colspan="2"></td>
<td>
<button type="submit" id="btnCalculate" class="btn btn-success">Calculate</button>
<button type="button" id="btnReset" class="btn btn-secondary">Reset</button>
</td>
</tr>
</tbody>
</table>
</form>
最合适的回答,由SO网友:Rika 整理而成
我更改了诸如“Chethan”和“Jacob”之类的文件。我编辑活动主题functions.php 文件而不是wp-admin->admin-ajax.php.然后更改ajaxURL
footer.php
<script>
var ajax_url = "<?php echo admin_url( \'admin-ajax.php\' ); ?>";
jQuery(function() {
jQuery("#calc_form").on("submit", function(e) {
// alert("submit");
e.preventDefault();
jQuery.ajax({
method: "POST",
url: ajax_url,
data: {
action: \'calculation\',
message_id: jQuery(\'#tot_tax_three_month_Input\').val()
},
dataType: \'json\',
success: function (output) {
console.log(output);
}
});
});
});
</script>
function.php 之前
admin-ajax.php // register the ajax action for authenticated users
add_action(\'wp_ajax_calculation\', \'calculation\');
// register the ajax action for unauthenticated users
add_action(\'wp_ajax_nopriv_calculation\', \'calculation\');
// handle the ajax request
function calculation() {
$value = $_REQUEST[\'message_id\'];
$value = 1 + 1;
echo json_encode(array("value" => $value));
die();
}
SO网友:Chetan Vaghela
您必须使用ajax函数的完整url。我有用处ajax_url
在您的Ajax Call - footer.php(theme footer) 文件代码。
<script>
var ajax_url = "<?php echo admin_url( \'admin-ajax.php\' ); ?>";
jQuery(function() {
jQuery("#calc_form").on("submit", function(e) {
// alert("submit");
e.preventDefault();
jQuery.ajax({
method: "POST",
url: ajax_url,
data: {
action: \'calculation\',
message_id: jQuery(\'#tot_tax_three_month_Input\').val()
},
dataType: \'json\',
success: function (output) {
}
});
});
});
</script>
从中删除代码
wp-admin->admin-ajax.php 文件并将该代码添加到活动主题的
functions.php 文件而不是
wp-admin->admin-ajax.php// handle the ajax request
function calculation() {
$value = $_REQUEST[\'message_id\'];
echo json_encode(array("value" => $value));
die();
}
// register the ajax action for authenticated users
add_action(\'wp_ajax_calculation\', \'calculation\');
// register the ajax action for unauthenticated users
add_action(\'wp_ajax_nopriv_calculation\', \'calculation\');