jquery - AJAX call I receive the “/wp-admin/admin-ajax.php 400 (Bad Request)” error
one text
Solution:
First, change
var jaxscript
to
var ajaxscript
var test = '667'
var ajaxscript = 'http://testing.local/wp-admin/admin-ajax.php'; //absolute ref for testing
$.ajax({
url: ajaxscript,
data: {
'action': 'php_tutorial',
'php_test': test
},
success: function(data){
console.log(data);
}
});
And use echo in place of return and use wp_die(); before closing function
function our_tutorial(){
$testing = 'not set';
if(isset($_REQUEST)){
$testing = $_REQUEST['php_test'];
}
echo json_encode($testing);
wp_die();
}
add_action('wp_ajax_php_tutorial', 'our_tutorial');
add_action( 'wp_ajax_nopriv_php_tutorial', 'our_tutorial' );
Source