Drupal Ajax 缓存
我正在更新我从 D6 编写的模块到 D7,因此必须在表单中用新的 #ajax 交换旧的 AHAH。
我正在执行一个 ajax 请求,该请求创建一个结果并用它替换一个表单元素。这基本上工作正常,但在第一个请求之后,结果被缓存并且不考虑表单中的更改。我认为这可能是浏览器问题,但 Drupal 是否会发送一个过期标头,导致浏览器采用缓存版本?还有其他想法吗?
hook_cache() 中的片段:
$form['fieldset']['mybutton'] = array(
'#type' => 'button',
'#value' => t('Send request'),
'#ajax' => array(
'callback' => 'mycallback',
'wrapper' => 'mywrapper',
'method' => 'replace',
'effect' => 'fade',
)
回调的片段:
function mycallback($form, $form_state) {
[..]
$form['fieldset']['mywrapper']['#markup'] = 'test';
return $form['fieldset']['mywrapper']['#markup'];
}
I'm updating a module I've written from D6 to D7, and therefore have to exchange the old AHAH by the new #ajax in forms.
I'm performing an ajax request which creates a result and replaces a form element with it. This basically works fine, but after the first request, the result is cached and does not take changes in the form into account. I think this is probably a browser-issue, but could it be that Drupal sends an expiration header which induces the browser to take the cached version? Ot any other ideas?
The fragment in hook_cache():
$form['fieldset']['mybutton'] = array(
'#type' => 'button',
'#value' => t('Send request'),
'#ajax' => array(
'callback' => 'mycallback',
'wrapper' => 'mywrapper',
'method' => 'replace',
'effect' => 'fade',
)
A snippet of the callback:
function mycallback($form, $form_state) {
[..]
$form['fieldset']['mywrapper']['#markup'] = 'test';
return $form['fieldset']['mywrapper']['#markup'];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经遇到过这个问题好几次了,这不是缓存问题。问题是你最初会有一个
包裹在你的
mywrapper
元素中,但是在你的 ajax 回调中你用一个字符串替换它......因此,包装器被替换,下次按下按钮时,脚本找不到需要替换的
,因为它不再存在了!
此外,
mycallback
函数的参数需要通过引用传递,因此请将签名更改为:function mycallback(&$form, &$form_state) {
。尝试让您的代码看起来更像这样:
如果有疑问,请查看示例模块 ,特别是
ajax_example
模块中的ajax_example_submit_driven_ajax()
示例。I've run into this problem a few times and it isn't a caching issue. The problem is you'll originally have a
<div>
wrapped around yourmywrapper
element, but in your ajax callback you're replacing it with a string...the<div>
wrapper is therefore replace and next time you press the button the script can't find the<div>
it needs to replace as it's not there any more!Also, the arguments for your
mycallback
function need to be passed in by reference so change the signature to this:function mycallback(&$form, &$form_state) {
.Try making your code look a bit more like this:
If in doubt have a look at the examples module, specifically the
ajax_example_submit_driven_ajax()
example in theajax_example
module.在第一个答案的帮助下找到了
答案再次添加包装器
我不认为你必须使用指针(&)至少drupal示例模块不需要
Figured it out with help of the first answer
Add wrapper again
And i don't think that u have to use the pointers (&) at least the drupal example module doesn't