2016-12-29 12 views
0

私は1 CLASと1つの.PHPファイルを持って、それはそれは私が他のクラスで呼び出しています私の方法です.PHPファイルだのfunctions.php AJAXは、ワードプレスの.phpファイル

FIRST ONE呼び出されていません。

//PHP 
add_action('wp_ajax_getJSONCurrencies', 'getJSONCurrencies'); 
add_action('wp_ajax_nopriv_getJSONCurrencies', 'getJSONCurrencies'); 

function getJSONCurrencies() { 
     $endpoint = $_POST['endp']; 
     $access_key = $_POST['access']; 
     $currencies = $_POST['curren']; 
     $source = $_POST['sour']; 
     $format = 1; 

     // Initialize CURL: 
     $ch = curl_init('https://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&currencies='.$currencies.'&source='.$source.'&format='.$format.''); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

     // Store the data: 
     $json = curl_exec($ch); 
     curl_close($ch); 

     // Decode JSON response: 
     echo json_decode($json, true); 

    } 

私の最初のクラスの関数を呼び出す2番目のクラスのカスタムwc-widget ... php。それは、エコーPHP内だから

jQuery(document.body).bind(\'bejob_price_slider_create bejob_price_slider_slide\', function(event, min, max) { 

     var simMoneda = jQuery(\'.woocommerce-currency-switcher-form > select option:selected\').text(); 
     var simbolo = simMoneda.split(","); 

     simMoneda = simbolo[1].trim(); 

     var endpoint = \'live\'; 
     var access_key = \'0a85b3479a1a24ceaba1d04f030e7028\'; 
     var currencies = \'USD,ARS,CLP,PEN,COP,MXN,PAB\'; 
     var source = \'EUR\'; 

     jQuery.ajax({ 
      type:\'POST\', 
      url: \'ajaxurl\', 
      //url: \'https://apilayer.net/api/\' + endpoint +\'?access_key=\' + access_key + \'&currencies=\' + currencies + \'&source=\' + source + \'&format=\' + 1, 
      data: { 
       endp: endpoint, 
       access: access_key, 
       curren: currencies, 
       sour: source, 
       action: \'getJSONCurrencies\' 
      }, 
      dataType: "json", 
      success: function(data) { 
       debugger; 
       console.log(data); 
      } 
     }); 

     if (min == 0) { 
       jQuery(\'.bejob_price_slider_amount span.from\').html("Gratis"); 

     } else { 
       jQuery(\'.bejob_price_slider_amount span.from\').html(min + simMoneda); 

     } 
     jQuery(\'.bejob_price_slider_amount span.to\').html(max + simMoneda); 

     jQuery(document.body).trigger(\'price_slider_updated\', [ min, max ]); 
    }); 

この第二のクラスは\」のシンボルを持って、私の考えは、私の関数にが、その後、Ajaxの方法から成功機能で受信getJSONCurrenciesをAJAX文でデータを送信することですJSONオブジェクト私は自分のWebページで使用します。

問題は、私がAjaxメソッドに入力しないデバッガを実行して、なぜそこに入っていないのかわかりません。

+0

は、外部JavaScriptファイルにあなたのjavascriptを移動します。データ(エンドポイント、アクセスキー、通貨など)を設定するには、[wp_localize_script](https://codex.wordpress.org/Function_Reference/wp_localize_script)を使用します。あなたはそれをPHPエコーに埋め込む必要はありません。これにより、コードのデバッグ/保守が大幅に簡素化されます。 (ここに良い書き方があります:https://pippinsplugins.com/use-wp_localize_script-it-is-awesome/) –

+0

そしてサイドノートとして、あなたはこれらの「クラス」を呼んでいますが、[クラス]ではありません( http://php.net/manual/en/language.oop5.basic.php)。 –

+0

ええ、私はクラスではないことを知っていますが、私は両方の.phpファイルを区別するためにこの用語を使用します。 – fiticida

答えて

0

どのようにjqueryを呼び出しますか?別の機能がある場合は、質問を編集してください。

はwp_localize_scriptを使用してコード以下のようにしてみてください:

add_action('wp_ajax_getJSONCurrencies', 'getJSONCurrencies'); 
add_action('wp_ajax_nopriv_getJSONCurrencies', 'getJSONCurrencies'); 

wp_enqueue_script('getJSONCurrencies', get_template_directory_uri().'/js/getJSONCurrencies.js', array('jquery'), '1.0', true); 
wp_localize_script('getJSONCurrencies', 'ajax_var', array(
    'url' => admin_url('admin-ajax.php'), 
    'nonce' => wp_create_nonce('ajax-nonce') 
)); 
関連する問題