I'm currently using a GraphQL API with Ajax & PHP on Worpress.
First things first, everything is working as well as expected on my browser/account. But as soon as I change my google user or different browser or going on my phone. My ajax request get a 400 from my admin-ajax.php.
Honestly, I have no idea what is the problem, I'm just doing a simple request on php with ajax, then I make a GraphQL query in my back, then I send it back in JSON format in my front. That's all. I have 400 bad request w/e I try to do in my back. I have no clue what to do neither where to look for.
This is one of my ajax request.
ajaxurl is defined no worries:)
$.ajax({
type: 'post',
url: ajaxurl,
data: {
'action': 'get_content_wino'
},
success: function(data){
console.log(data);
console.log("fetch ok")
},
error: function(errorThrown) {
console.log(errorThrown);
}
});
This is my back.
function get_content_wino()
{
if ( isset($_REQUEST) )
{
echo read_file();
die();
}
}
add_action( 'wp_ajax_get_content_wino' , 'get_content_wino' );
w/e my action is I get 400. on admin-ajax.php
Quick reminder, everything is perfectly working as long as I have the same browser and google accounts. There is no ip whitelist or login account on my website, pure ajax and php here.
Thanks
CodePudding user response:
There are two action hooks when you hook into wp_ajax
.
wp_ajax_{$action}
for for logged-in users. Docs
and
wp_ajax_nopriv_{$action}
for logged-out users. Docs
So to combine the two, you could use the following code:
add_action( 'wp_ajax_get_content_wino' , 'get_content_wino' );
add_action( 'wp_ajax_nopriv_get_content_wino' , 'get_content_wino' );
function get_content_wino()
{
if ( isset($_REQUEST) )
{
echo read_file();
die();
}
}