2015-10-23 4 views
5

Ho problemi con wp_remote_get nel mio plugin Wordpress.La chiamata Ajax non riesce quando si utilizza wp_remote_get nel plugin wordpress

Quello che voglio fare è chiamare un metodo all'interno della mia classe pubblica principale con ajax. Ma il fatto è che la chiamata fallisce quando viene utilizzata la funzione . Dovrebbe fare una chiamata API e restituire i dati a jQuery. Quando commento la wp_remote_get, la chiamata funziona correttamente e la risposta viene restituita. Qualche idea come posso fare questo lavoro?

metodo che elabora la chiamata:

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) { 
      //do something with this data later on 
      var result = jQuery.parseJSON(data); 
      } 
     }); 
    }); 
}); 

azioni Wordpress sono tutti registrati bene perché la chiamata funziona quando io non uso il wp_remote_get

EDIT: La soluzione era più che semplice, avevo solo bisogno di aggiungere e.preventDefault();

risposta

1

È necessario aggiungere errori durante il controllo del codice. Questo può aiutarti a capire che cosa sta causando il problema.

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); 
    if (is_wp_error($response)) { 
     $error_code = $response->get_error_code(); 
     $error_message = $response->get_error_message(); 
     $error_data = $response->get_error_data($error_code); 
     // Process the error here.... 
    } 
    echo $response; 
    die(); 
    } 
} 

Inoltre si utilizza eco su wp_remote_get risultato. Come definito nella documentazione, wp_remote_get ritorna a WP_Error o all'istanza dell'array. Quindi dovresti usare qualcosa del tipo:

echo $response['body']; 
+0

Il problema è che la chiamata ajax fallisce completamente se wp_remote_get è incluso, non appena lo rimuovo la chiamata funziona bene. Sto pensando che potrebbe essere un qualche tipo di conflitto. – Danijelb

+0

Bene ... che devi abilitare la segnalazione degli errori nella tua configurazione php e impostare il livello di segnalazione degli errori per il debug. Questo ti porterà errore esatto per andare oltre. 'error_reporting (E_ALL); ini_set ("display_errors", 1); ' – alexeevdv