drupal调用ajax函数
$delete_commant = l(t('X'), $url, array('attributes' =>; array('onclick' => '返回 call_ajax(123)')));
当我在 call_ajax 函数定义中添加警报时,它会收到警报,因此我继续编写 ajax post 方法,但是当我单击链接页面刷新时,
如何在 drupal 中实现 ajax
function call_ajax(str){alert(str);
var url = "post.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
}
$delete_commant = l(t('X'), $url, array('attributes' =>
array('onclick' => 'return call_ajax(123)')));
when i put alert in call_ajax function definition, it is alerted, so i proceed to write the ajax post method, but when i click the link page getting refresh,
How to implement ajax in drupal
function call_ajax(str){alert(str);
var url = "post.php";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
drupal默认使用jquery。那么为什么不直接使用 http://api.jquery.com/jQuery.ajax/。
其他技术涉及使用 form_api,例如 ahah : http ://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#ahah
drupal uses jquery by default. So why not just implement this call using http://api.jquery.com/jQuery.ajax/.
Other techniques involve using the form_api like the ahah : http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#ahah
我认为你在 onclick 处理程序中返回 false 。但是您应该学习更多关于 jQuery 和不引人注目的 JavaScript。
I think you have return false in your onclick handler. But you should learn a bit more about jQuery and Unobtrusive JavaScript.