[英]Drupal 7 AJAX Call via hook_menu
因此,无论何时有人选择新的送货方式,我都希望在Ubercart Checkout页面上更新我的新窗格。 到目前为止,选择新的送货方式后,每次刷新页面时,我都可以使这种情况发生。 下一步是通过AJAX使它运行。
我的问题是ajax命令没有从AJAX回调中触发。 div未更新。 现在我只有一些简单的文字。 稍后,我将添加所需的实际表单信息,这将是另一个问题。 但是,我什至无法使这个测试用例正常工作。
我正在为Drupal的Ubercart FedEx模块开发一项新功能。 我已经为此工作了一段时间,但无济于事。 我尝试了很多不同的东西,没有用。
非常清楚...我知道ajax调用正在触发。 我什至可以将成功附加到ajax $ .get调用中并获取控制台输出,并设置drupal变量。 仅div替换代码不起作用。 此难题的所有其他作品。
我进入ajax调用并返回javascript。 我什至可以在JS的ajax调用上附加一个成功函数,并在成功时获取控制台输出。 javascript不是问题。
// Implements hook_menu()
function uc_fedex_menu() {
$items = array();
$items['uc_fedex_jquery_callback/%'] = array(
'title' => 'My Custom Callback',
'description' => 'Listing of blogs.',
'page callback' => 'uc_fedex_calendar_jquery_callback',
'page arguments' => array(1),
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
// AJAX callback: Updates calendar
// THIS IS WHERE THE ERROR IS... the variable is set
// But the div content is never replaced.
function uc_fedex_calendar_jquery_callback($argument) {
variable_set('uc_fedex_shipment_type',$argument);
$commands[] = ajax_command_replace('#fromdate', "works");
return array('#type' => 'ajax', '#commands' => $commands);
}
// Actual UI of Calendar Pane
function uc_fedex_uc_checkout_pane_shipdate($op, $order, $form = NULL, &$form_state = NULL) {
switch ($op) {
case 'view':
// Check for shipping quote option without altering Ubercart Core.
// The $.get line makes the hook_menu call which in turn
// makes the call back to the above function that has the issue
drupal_add_js("
jQuery(function ($) {
$(document).ready(function() {
last = 'o';
setInterval(function() {
$('#quote input:radio:checked').each(function() {
if($(this).val() != last){
last = $(this).val()
$.get('https://www.fdanconia.com/uc_fedex_jquery_callback/'+ $(this).val());
}
});
}, 500);
});
});", 'inline');
$contents['calendar'] = array(
'#type' => 'textfield',
'#title' => t('Choose Your Estimated Arrival Date'),
'#default_value' => date('m/j/Y',$response[1]),
'#prefix' => '<div id="fromdate">',
'#suffix' => '</div>',
);
return array('description' => $description, 'contents' => $contents);
}
}
// Implements hook_uc_checkout_pane().
function uc_fedex_uc_checkout_pane() {
$panes['calendar'] = array(
'callback' => 'uc_fedex_uc_checkout_pane_shipdate',
'title' => t('Shipping Calendar'),
'desc' => t('A calendar to allow customers to choose shipping date.'),
'process' => TRUE,
);
return $panes;
}
关于js的注释jQuery(function same_func(){})等效于$(document).ready(function same_func(){})
因此,外部jQuery()调用将一个函数绑定到文档准备就绪。 该函数在文档就绪时触发,并将另一个函数绑定到已触发的文档就绪。
在Drupal中,大多数时候变量$都是不附加的,因此您必须显式创建一个以$形式传入jQuery的方法(函数)。
(function($){
$ available in here
})(jQuery);
Think of the the above as:
(function)(variables) where the function takes a variable $ ..
Or you can think of it like this:
function test ($) {
$(jquery magic).etc()'
}
test(jQuery);
..除了功能测试外,功能调用都包含在“相同”行中。
检查萤火虫/控制台,查看是否正在调用$ .get。
另一个调试是在uc_fedex_calendar_jquery_callback()中使用php的error_log('message')并查看Apache error.log。
我最终在响应回调中添加了我需要的参数,然后在原始调用的AJAX响应中处理它们。 我只是使用jQuery手动使用更新的变量数据来操作DOM。 我认为没有其他方法可以不改变Ubercart核心,这是不可接受的。
如果有人想要我用来解决此问题的代码,只需询问一下,您就可以收到。 (这很长,但是如果有人愿意,我会在这里发布。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.