0

On my plugin admin page, I have make an ajax request to admin-ajax.php. For some reason, once the request is made, another request is made to admin.php which directs me away from my current page...

Here's my javascript:

  var request = new XMLHttpRequest();
  request.open('POST', ajaxurl, true);
  request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');

  request.onload = function() {
    if (request.status >= 200 && request.status < 400) {
      // Success!
      document.getElementById('messages').innerHTML = '<p>' + request.responseText + '</p>';
    } else {
      document.getElementById('warnings').innerHTML = '<p>' + request.responseText + '</p>';
    }
  };
  request.send(serialise(data));

That request works fine, it hits my callback and the relevant data gets passed back to the page. However, I'm almost instantly making a separate request to admin.php automagically and it's directing me away from the current page.

requests

As you can see, the request to admin.php takes the parameters passed to the admin-ajax.php, excluding the action which suggests that the form I'm taking input from is being submitted, even though is has no submit button.

<form id="add-property-form">
  [all my inputs]
  <button onClick="addProperty()">Add property</button>
</form>
1

As suspected, Wordpress was treating the button as type="submit" fixed by doing:

<button type="button" onClick="addProperty()">Add property</button>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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