I'm trying to pass a JS variable to PHP via ajax but keep getting a 400 error. https://www.youtube.com/watch?v=DXK9XDN9puY
MAMP on MacOS
<script type="text/javascript">
jQuery(document).ready(function() {
var test = '667'
//console.log(test)
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
data: {
'action': 'php_tutorial',
'php_test': test
},
success: function(data) {
console.log("Happy")
}
});
});
</script>
<?php
function our_tutorial()
{
if (isset($_REQUEST)) {
$testing = $_REQUEST['php_test'];
echo 'This is our JS Variable :' . $testing;
}
die();
}
add_action('wp_ajax_php_tutorial', 'our_tutorial');
I must be missing something, somewhere...
Errors: POST https://ezy-rebuild:8890/wp-admin/admin-ajax.php Status 400 Bad Request VersionHTTP/1.1 Transferred519 B (1 B size) Referrer Policystrict-origin-when-cross-origin DNS ResolutionSystem
Cache-Control no-cache, must-revalidate, max-age=0 Connection close Content-Type text/html; charset=UTF-8 Date Fri, 25 Aug 2023 05:35:50 GMT Expires Wed, 11 Jan 1984 05:00:00 GMT Referrer-Policy strict-origin-when-cross-origin Server Apache/2.4.54 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141 OpenSSL/1.0.2u mod_wsgi/3.5 Python/2.7.18 Transfer-Encoding chunked X-Content-Type-Options nosniff X-Frame-Options SAMEORIGIN X-Powered-By PHP/7.4.33 X-Robots-Tag
Accept / Accept-Encoding gzip, deflate, br Accept-Language en,en-US;q=0.5 Connection keep-alive Content-Length 32 Content-Type application/x-www-form-urlencoded; charset=UTF-8
Host ezy-rebuild:8890 Origin https://ezy-rebuild:8890 Referer https://ezy-rebuild:8890/testie/