I am having problems with wp_remote_get in my Wordpress plugin.
What I want to do is call the method inside my main public class using ajax. But the fact is that the call fails when it uses the wp_remote_get function. It is assumed that it makes an API call and returns data in jQuery. When I comment on wp_remote_get , the call is working fine and the response is returned. Any ideas how I can make this work?
Method that processes the call:
public function countryLookupApiCall() { if (isset($_POST['action']) && isset($_POST['country'])) { $country = $_POST['country']; $apiKey = $this->getApiKey(); $url = $this->url . $country . '/callCharges?apiKey=' . $apiKey . '&response=JSON'; $response = wp_remote_get($url); echo $response; die(); } }
JQuery
jQuery(document).ready(function() { jQuery("#countryLookupForm").submit(function(e){ var country = jQuery("#selectCountry").val(); var action = 'countryLookupResponse'; jQuery.ajax ({ type: 'POST', url: countryLookup.ajaxurl, dataType: 'json', data: {action: action, country: country}, success: function(data) {
All Wordpress actions are logged well, because the call works when I do not use wp_remote_get
EDIT: The solution was more than simple, I just needed to add e.preventDefault();
source share