Ajax call not being able to return sent data
-
Hi there,
I’ve made a couple of plugins so far, including ajax requests. I’ve always used jQuery for that, but since I’m learning to use some more pure JS, I’ve ran into trouble.
I’m trying to make a POST request to the plugin it’s core file like:const params = { 'action': 'test_method', 'name' : 'Jan', 'age' : '25', }; const options = { method: 'POST', body: JSON.stringify( params ), }; fetch(wp.ajax_url+'?action=test_method', options) .then(response => response.json()) .then(data => console.log(data));
As you can see I tried sending the action through the url and through the body.
And as simple as this function, I just try to return the name that I’ve sent. But it’s returningnull
:add_action('wp_ajax_nopriv_test_method', 'test_method'); add_action('wp_ajax_test_method', 'test_method'); function test_method(){ $test = $_REQUEST['name']; wp_send_json($test); // wp_send_json('test'); }
Does anyone know what I’m doing wrong?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Ajax call not being able to return sent data’ is closed to new replies.