0

カードモジュール

drupal_add_js('jQuery(document).ready(function () {

  currentRequest = $.ajax({
  timeout:0,
  cache: false,
  url: pageUrl,
  dataType: "json",
  type: "GET",
  success: function(data){
  $("#edit-field-currency-type-und-0-value").val(data.currency);

  }

});

ajaxリクエストモジュール

$items['mccurr/%'] = array(
'title' => '', 
'page callback' => 'ajax_currency_type', 
'access arguments' => array('access content'), 
'page arguments' => array(1),
'type' => MENU_SUGGESTED_ITEM,
);

function ajax_currency_type($ccode){
 drupal_add_http_header('Content-Type', 'application/javascript; utf-8');
$query = "SELECT countries_country.currency 
        FROM countries_country
        WHERE countries_country.iso2 = '".$ccode."'";
$data = db_query($query);

return  drupal_json_encod($data);
}

この方法でjsonデータを返すのは正しいですか?それ以外の場合、car.moduleで戻りデータを取得するにはどうすればよいですか?

ありがとうございました

4

1 に答える 1

1

ページのコールバック関数でprintは、文字列をingdrupal_json_encodeではなく 'd にします。return

print drupal_json_encode($data);
exit;

モジュールには、最初に理解する必要があるセキュリティ上の問題があることに注意してください。

<?php
$items['mccurr/%'] = array(
  'title' => '', 
  'page callback' => 'ajax_currency_type', 
  'access arguments' => array('access content'), 
  'page arguments' => array(1),
  'type' => MENU_SUGGESTED_ITEM,
);

function ajax_currency_type($ccode){
  drupal_add_http_header('Content-Type', 'application/javascript; utf-8');
  $query = 'SELECT currency FROM countries_country WHERE countries_country.iso2 = :code'; // doesn't matter multi lines 
  $data = db_query($query, array(':code' => $ccode)); // Parameters, baby!
  // You will probably need to fetchAllAssoc() or something to get the data in the desired format. Also try to send proper headers on empty results, etc
  print  drupal_json_encode($data);
  exit;
}

このコードを使用すると、モジュールは json でエンコードされた文字列を出力し、他の HTML は出力に含まれません。これは最善の方法ではないことに注意してください。system/ajaxpathのメニュールーター定義を参照し、delivery callbackそこに注意してください。

于 2013-06-28T16:49:40.820 に答える