2015-10-23 13 views
5

Wordpressプラグインでwp_remote_getの問題が発生しています。ワードプレスプラグインでwp_remote_getを使用すると、Ajax呼び出しが失敗する

私がしたいことは、ajaxを使用して、私のメインパブリッククラスの中でメソッドを呼び出すことです。しかし、問題は、wp_remote_get関数が使用されているときに呼び出しが失敗することです。 API呼び出しを行い、データをjQueryに返すことになっています。私がwp_remote_getをコメントアウトすると、呼び出しは正常に動作し、応答が返されます。どのように私はこの仕事をすることができますどのようなアイデア?

コールを処理する方法:

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の:私はwp_remote_get

EDITを使用していないときの呼び出しが動作するため

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); 
      } 
     }); 
    }); 
}); 

はWordpressのアクションはすべてが順調に登録されています。 解決策は単純ではありませんでした。追加する必要がありました。e.preventDefault();

答えて

1

コードにエラーを追加する必要があります。これにより、問題の原因を突き止めるのに役立ちます。また、あなたはを使用している

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(); 
    } 
} 

wp_remote_get結果にをエコー。ドキュメンテーションwp_remote_getで定義されているように、WP_Errorまたは配列インスタンスを返します。

echo $response['body']; 
+0

問題はAJAX呼び出しが完全に失敗したwp_remote_getが含まれている場合は、できるだけ早く私はそれを削除するよう呼び出しがうまく動作することです。ですから、このようなものを使用する必要があります。私はそれが何らかの矛盾であると思っています。 – Danijelb

+0

さて、PHPの設定でエラー報告を有効にし、エラー報告レベルをデバッグする必要があります。これにより、正確なエラーが表示され、さらに移動することができます。 'error_reporting(E_ALL); ini_set( "display_errors"、1); ' – alexeevdv

関連する問題