Reputation: 385
I have been troubleshooting this for hours now and I am stumped as to why this error is occurring.
in my JS I have the following ajax:
var $ = jQuery; // define jquery
let name = "Mark";
let pies = "cherry";
$(document).ready(function(){
$.ajax({
type : 'POST',
dataType : 'json',
url : ajaxurl,
nonce: nonce,
data : {
action: 'myfunction',
name: name,
pies: pies,
nonce: nonce
}
}).done(function(response) {
console.log(response);
});
}); // end document ready
and in my plugin's PHP, I have
function myfunction() {
if ( wp_verify_nonce( $_POST['nonce'], 'registration_nonce')) {
$name = $_POST['name'];
$pies = $_POST['pies'];
file_put_contents('debug_output.txt', $pies);
die();
}
}
add_action( 'wp_ajax_myfunction', 'myfunction' );
add_action( 'wp_ajax_nopriv_myfunction', 'myfunction');
This gives me a 400 every single time. This should be a simple thing and I normally do this all of the time but for some reason, I am seriously missing something. Any ideas anyone? I whittled it done to the bare minimum and still no luck. Both the ajax URL and nonce are fine. Did something in WP change?
Upvotes: 0
Views: 409
Reputation: 2338
Typo?
add_action( 'wp_ajax_nopriv_myactiont', 'myaction');
// should be
add_action( 'wp_ajax_nopriv_myaction', 'myaction');
// final result
function myaction(){
echo "success";
exit;
}
add_action( 'wp_ajax_myaction', 'myaction' );
add_action( 'wp_ajax_nopriv_myaction', 'myaction');
Diego's answer has some good points to check too.
Upvotes: 0
Reputation: 1716
There are some things that maybe you should fix/check:
Upvotes: 1