我不断遇到此错误:
POST http://localhost/wptest2/wp-admin/admin-ajax.php 400 (Bad Request)
(anonymous) @ VM12565:1
send @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
ajax @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
(anonymous) @ options-general.php?page=fvc-settings:784
dispatch @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3
r.handle @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3
- 在下面的代码中,ajaxpost((挂在我的管理员页面上插入。
- 单击jQuery选择的按钮时,它应启动注册的邮政请求并返回简单的响应。
这不是jquery.post((或$ .ajax((发生的事情。我尝试了下面的评论中提到的几件事,但无法正常工作,有人可以说出这个问题吗?
function ajaxPost() {
?>
<script>
jQuery(document).ready(function($) {
$('#previousButton').click(function() {
var postData = {
action : 'selectTargetsWithOffset',
data : {
offsetMultiplier : 1,
incrementSize : 10
},
success : function(response) {
console.log(`=====jQuery success=====`);
}
};
var handleResponse = function(response) {
console.log(response, `=====response=====`);
};
jQuery.post(ajaxurl, postData, handleResponse);
// this code also threw a 400 error when
// I commented out the above code instead
// $.ajax({
// type: "POST",
// url: ajaxurl,
// dataType: "json",
// data: {
// action: "selectTargetsWithOffset",
// },
// success: function() {
// console.log(response, `=====response
// from $.ajax}=====`);
// }
// });
})
})
</script>
<?php
}
add_action('admin_footer', 'ajaxPost');
function selectTargetsWithOffset() {
wp_send_json([ 'message' => 'from back end' ]);
wp_die();
}
add_action('wp_ajax_selectTargetsWithOffset', 'selectTargetsWithOffset');
也尝试了 - 进入Admin-Ajax.php并将响应代码从400更改为420,以查看是否会做任何事情。我确实遇到了一些反映更改的Wierd错误。
if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( "wp_ajax_{$action}" ) ) {
wp_die( 'Test', 420 );
}
- 在wp_die((的arg1中将'0'更改为"测试"并在浏览器中访问此URL:http://localhost/wptest2/wp--admin/admin/admin-ajax.php。我仍然在此地址在页面上看到0渲染
将以下内容添加到Admin-Ajax.php的顶部:
?>
<script>
console.log(`=====TEST=====`);
var request = "<?php echo $_REQUEST ?>";
console.log(request['action'], `=====request=====`);
</script>
<?php
当我尝试在管理区域中触发Ajax调用时,我仍然只能获得400个不良响应控制台日志。但是,当我直接导航到URL时,现在我看到===测试===语句和请求被标记为未定义(我希望(。我认为这是关于我如何称呼ajax请求的意思。
这是我的HTML代码,用于进行调用的按钮:
<button id="previousButton">Previous</button>
响应简化的回调建议
这是控制台日志:
=====jQuery success=====
VM13659:1 POST http://localhost/wptest2/wp-admin/admin-ajax.php 400 (Bad Request)
(anonymous) @ VM13659:1
send @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
ajax @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
n.(anonymous function) @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
(anonymous) @ options-general.php?page=fvc-settings:785
dispatch @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3
r.handle @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3
这是代码:
function ajaxPost() {
?>
<script>
jQuery(document).ready(function() {
jQuery('#previousButton').click(function() {
var postData = {
action : 'selectTargetsWithOffset',
data : {
offsetMultiplier : 1,
incrementSize : 10
},
success : function(response) {
console.log(`=====jQuery success=====`);
}
};
var handleResponse = function(response) {
console.log(response, `=====response=====`);
};
jQuery.post(ajaxurl, postData, handleResponse);
});
})
</script>
<?php
}
add_action('admin_footer', 'ajaxPost');
function selectTargetsWithOffsetcb() {
print_r($_POST);
die();
}
add_action( 'wp_ajax_selectTargetsWithOffset', "selectTargetsWithOffsetcb");
add_action( 'wp_ajax_nopriv_selectTargetsWithOffset', "selectTargetsWithOffsetcb" );
页面本身没有print_r((输出,它保持不变,没有任何刷新
值得注意的是,admin-ajax.php中的调试代码====TEST===
未能记录。我认为这表明由于某种原因,该请求无法到达该文件(因为在浏览器中导航到该文件确实会导致其登录到控制台(
也尝试了此操作,但也有400个错误:
function ajaxPost() {
?>
<script>
jQuery(document).ready(function() {
jQuery(document).on("click","#previousButton", function() {
jQuery.ajax({
url: "<?php echo admin_url( 'admin-ajax.php' ) ?>",
data: {"action":"selectTargetsWithOffset","datas": {"name": "yourfields"}},
type:"post",
success: function(data) { alert(data); }
})
});
})
</script>
<?php
}
add_action('admin_footer', 'ajaxPost');
我认为您尚未在插件或功能文件中声明回调函数。
请在插件或FN文件中添加以下代码并进行测试。
add_action( 'wp_ajax_selectTargetsWithOffset', "selectTargetsWithOffsetcb");
add_action( 'wp_ajax_nopriv_selectTargetsWithOffset', "selectTargetsWithOffsetcb" );
function selectTargetsWithOffsetcb()
{
print_r($_POST);
die();
}
也更新此JS
<script>
jQuery(document).on("click","#previousButton",function(){
jQuery.ajax({
url:"<?php echo admin_url( 'admin-ajax.php' ) ?>",
data:{"action":"selectTargetsWithOffset","datas":{"name":"yourfields"}},
type:"post",
success:function(data){alert(data);}
})
});
</script>