我不断收到此错误:
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
这不是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,以查看它是否可以执行任何操作。我确实收到一些反映更改的奇怪错误。
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 );
}
在admin-ajax.php的顶部添加以下内容:
?>
<script>
console.log(`=====TEST=====`);
var request = "<?php echo $_REQUEST ?>";
console.log(request['action'], `=====request=====`);
</script>
<?php
当我尝试在管理区域中触发ajax调用时,我仍然仅收到400错误响应控制台日志。但是,当我直接导航到URL时,现在我看到=== TEST ===语句,并且请求被标记为未定义(正如我期望的那样)。我认为这意味着我如何称呼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');
答案 0 :(得分:0)
我认为您尚未在插件或函数文件中声明回调函数。
请在您的插件或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>