我如何修复关于 admin-ajax.php 的 400(错误请求)错误
how do i fix 400 (Bad Request) error about admin-ajax.php
我想通过 JS 传递 html 值给 PHP 处理,但我仍然得到错误,400(错误请求)。
这是HTML:
</div>
<form class="row g-3" id="my_form">
<div class="col-md-12">
<label for="url" class="form-label">Input URL</label>
<input type="text" class="form-control my_info" id="url" required>
<div class="col-12">
<button class="btn btn-primary submit" type="button">Submit form</button>
</div>
</form>
</div>
这是JavaScript:
function getBaseUrl() {
var re = new RegExp(/^.*\//);
var my_local_url = re.exec(window.location.href)[0];
return my_local_url;
}
console.log(getBaseUrl())
jQuery(document).ready(function($) {
console.log('test');
// We'll pass this variable to the PHP function example_ajax_request
var my_data = {
'action':'crawler_info',
'my_info' : document.getElementById('url').value
};
$('.submit').click(function (){
// This does the ajax request
$.ajax({
url: getBaseUrl() + 'admin-ajax.php',
data: my_data,,
success:function(data) {
// This outputs the result of the ajax request
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
})
});
这是PHP:
function get_info() {
// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {
$infos = $_REQUEST['my_info'];
// Now we'll return it to the javascript function
// Anything outputted will be returned in the response
echo $infos;
// If you're debugging, it might be useful to see what was sent in the $_REQUEST
print_r($_REQUEST);
}
// Always die in functions echoing ajax content or it will display 0 or another word
die();
}
add_action( 'wp_ajax_crawler_info', 'get_info' );
add_action( 'wp_ajax_nopriv_crawler_info', 'get_info' );
我尝试了很多方法修复它,但它仍然再次显示此错误。
有谁知道如何解决我的问题,谢谢
我已经解决了我的问题,我发现一些代码放错了地方
我想通过 JS 传递 html 值给 PHP 处理,但我仍然得到错误,400(错误请求)。
这是HTML:
</div>
<form class="row g-3" id="my_form">
<div class="col-md-12">
<label for="url" class="form-label">Input URL</label>
<input type="text" class="form-control my_info" id="url" required>
<div class="col-12">
<button class="btn btn-primary submit" type="button">Submit form</button>
</div>
</form>
</div>
这是JavaScript:
function getBaseUrl() {
var re = new RegExp(/^.*\//);
var my_local_url = re.exec(window.location.href)[0];
return my_local_url;
}
console.log(getBaseUrl())
jQuery(document).ready(function($) {
console.log('test');
// We'll pass this variable to the PHP function example_ajax_request
var my_data = {
'action':'crawler_info',
'my_info' : document.getElementById('url').value
};
$('.submit').click(function (){
// This does the ajax request
$.ajax({
url: getBaseUrl() + 'admin-ajax.php',
data: my_data,,
success:function(data) {
// This outputs the result of the ajax request
console.log(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
})
});
这是PHP:
function get_info() {
// The $_REQUEST contains all the data sent via ajax
if ( isset($_REQUEST) ) {
$infos = $_REQUEST['my_info'];
// Now we'll return it to the javascript function
// Anything outputted will be returned in the response
echo $infos;
// If you're debugging, it might be useful to see what was sent in the $_REQUEST
print_r($_REQUEST);
}
// Always die in functions echoing ajax content or it will display 0 or another word
die();
}
add_action( 'wp_ajax_crawler_info', 'get_info' );
add_action( 'wp_ajax_nopriv_crawler_info', 'get_info' );
我尝试了很多方法修复它,但它仍然再次显示此错误。 有谁知道如何解决我的问题,谢谢
我已经解决了我的问题,我发现一些代码放错了地方