AJAX POST request – not sending.
-
Hi All,
I’ve been trying to find a piece of AJAX code that works, or how to layout my own that successfully sends and recieves a AJAX post request from my own custom plugin.
I started by using the code from here but I think it had something to do with the url missing. I added in ‘admin_url(‘admin-ajax.php’)’
Sorry for the code dump, but I have no idea what an where I am going wrong.
close_pressed and open_pressed are the php functions.
openShop() and closeShop() are JS functions.add_action("wp_ajax_open_pressed","open_pressed"); add_action("wp_ajax_close_pressed","close_pressed"); add_action("wp_ajax_nopriv_open_pressed","open_pressed"); add_action("wp_ajax_nopriv_close_pressed","close_pressed"); function exampleMenu() { echo <<< 'EOD' // //backend settings display // <h2> Is your Shop open?</h2> <p id="Status">The shop is Open</p> <button onclick='openShop()'>Indicate Open</button> <button onclick="closeShop()">Indicate Closed</button> // // // //Below is the issue // <script> function openShop(){ document.getElementById("Status").innerHTML = "The shop is Open"; jQuery(document).ready(function() { var data = { 'action': 'open_pressed', 'post_type': 'POST', 'name': 'Close the shop' }; jQuery.post("admin_url( 'admin-ajax.php' )" , data, function(response) { console.log( response ); }, 'json'); }); } function closeShop(){ document.getElementById("Status").innerHTML = "The shop is Closed"; jQuery(document).ready(function() { jQuery(document).ready(function() { var data = { 'action': 'close_pressed', 'post_type': 'POST', 'name': 'Close the shop' }; jQuery.post("admin_url( 'admin-ajax.php' )" , data, function(response) { console.log( response ); }, 'json'); }); } </script> EOD; } function open_pressed(){ $isOpen = true; setIsOpen($isOpen); $name = isset($_POST['name'])?trim($_POST['name']):""; $response = array(); $response['message'] = "Successfull Request"; echo json_encode($response); exit; } function close_pressed(){ $isOpen = false; setIsOpen($isOpen); $name = isset($_POST['name'])?trim($_POST['name']):""; $response = array(); $response['message'] = "Successfull Request"; echo json_encode($response); exit; } }
Note: The Changing of the <p id=’status’> occurs, The javascript does run.
When I check the chrome logs, no Successful request shows up nor does the $isOpen change, so I thus assume the problem lies within the jQuery.The page I need help with: [log in to see the link]
- The topic ‘AJAX POST request – not sending.’ is closed to new replies.