我在 JavaScript 中有两个数组: var xcoord = [];
和var ycoord = [];
经过一些处理后,每个数组包含 15 个数值。我想将此数据发送到Drupal中的菜单回调。
我的 AJAX 代码:
$.post('http://mysite.com/?q=menu_example/my_page',
{'ycoord[]': ycoord, 'xcoord[]': xcoord }
);
Drupal PHP:
$items['menu_example/my_page/%/%'] = array(
'title' => 'My Page',
'description' => 'i hope this works',
'page callback' => '_graphael',
'page arguments' => array(2, 3), // fill this,
'access callback' => true,
'type' => MENU_CALLBACK,
);
在我的控制台中,我看到 xcoord
和 ycoord
的值正在正确传输,但回调不起作用。 我的问题:如何将数组传递给菜单回调?我是否仍应在$items
键中使用%
占位符?
你传递给$.post()
的第二个参数没有附加到你作为第一个参数传递的 URL;它是在 $_POST
中传递给 PHP 的数据。
菜单回调的正确定义应为以下定义。
$items['menu_example/my_page'] = array(
'title' => 'My Page',
'description' => 'I hope this works',
'page callback' => '_graphael',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
然后,您的页面回调应该在 $_POST
中找到从 jQuery 传递的数据。