0

I am trying to create an ajax post listener to a wordpress page in a domain (let's just say http://example.com). Another web app in the same domain (http://example.com) exists and a user sends ajax posts from there.

I have implemented a simple page:

<?php
get_header();
?>
<h3>Listener</h3>
<div id="listener1">
</div>
<script type="text/javascript">
jQuery(document).ready(function($){
    console.log("Into WPTOASTER");
    $.ajaxSetup({
        success: function() {
            console.log("trapped?");
        }
    });
});
</script>
<?php
get_footer();
?>

that is located to http://example.com/listener .

In the same server, I have an http://example.com/zzz.php which has the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script type='text/javascript' src='http://example.com/wp-includes/js/jquery/jquery.js?ver=1.11.3'></script>
<script type='text/javascript' src='http://example.com/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
</head>
<body>
<form name="aaaa">
<input type="hidden" name="aaa" value="111">
<button type="button" id="salty">CLICK AND SUBMIT</button>
</form>
<script>
jQuery(document).ready(function($){
    $(document).on("click","#salty",function(ev){
        ev.preventDefault();
        $.post('http://example.com/listener',{"name":"john","lastname":"smith"},function(data){
            console.log("check if sent.");
        });
    });
});
</script>
</body>
</html>

When I use zzz.php to send ajax requests to listener I get a 404 error. The operation would be a simple notification once the php is received. Can anyone help me on this?

1 Answer 1

0

First, WordPress has a built-in way to handle AJAX requests, you should read this page and use that method instead.

The issue with your method is that you're POSTing the name var. name is a WordPress query var, and is causing WordPress to query for a page with slug John. Always prefix your vars with a unique string to avoid these problems.

8
  • Ok this was a bad example of data payload (They are not my full data). What I really want for the listener template page to do is actually "listen" to POST requests. I will be using later woocommerce to send new orders there instead of an email, without the need to refresh (this page would be protected or ideally it would be transferred to a panel in the admin area).
    – Panagiotis
    Nov 3, 2015 at 18:53
  • If not AJAX, then use the admin_post action to handle them. You're running a needless query to serve those requests with a page.
    – Milo
    Nov 3, 2015 at 18:56
  • Excuse me, but read again the requirement, without the need to refresh. It's something similar to the woocommerce desktop notifications plugin in codecanyon only I want to implement something different on top of it.
    – Panagiotis
    Nov 3, 2015 at 18:59
  • So what's the issue? Did you read the page about how to use AJAX with WordPress? Do that. That's what the plugin you're trying to replicate does.
    – Milo
    Nov 3, 2015 at 19:01
  • 1
    Maybe you want the Heartbeat API? Your code doesn't make much sense to me so I'm not sure what you're trying to accomplish, sorry.
    – Milo
    Nov 3, 2015 at 19:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.