我想发送一个ajax post请求到Drupal6中名为sampleTest的模块。 我已经尝试了一些代码,但我不知道如何将模块 url 放入 jquery ajax 函数中。
<script type="text/javascript">
$(function(){
$("#one").click(function(){
$.ajax({
type: 'POST',
url: 'http://localhost/drupal/www/admin/build/modules/module/get/contact',
dataType: 'json',
data: "test",
success:function(data) {
alert("123");
},
complete: function(data){
alert("complete");
}
});
});
});
</script>
最佳答案
您不能将 AJAX 请求发送到这样的模块,而是在模块中实现一个路径(使用 hook_menu()
),为该路径提供输出 AJAX 响应的页面回调,然后调用该路径在您的 AJAX 调用中。这是一个非常基本的示例:
function mymodule_menu() {
$items['ajax-test'] = array(
'access callback' => TRUE,
'page callback' => 'mymodule_ajax_callback',
'type' => MENU_CALLBACK
);
}
function mymodule_ajax_callback() {
$post_data = $_POST;
// Do something with the post data
$output = array('status' => 'OK');
// For Drupal 6
drupal_json($output);
exit();
// Or for Drupal 7, just in case you want to know
drupal_json_output($output);
drupal_exit();
}
那么你的 JS 看起来像这样:
$(function(){
$("#one").click(function(){
$.ajax({
type: 'POST',
url: '/drupal/ajax-test',
dataType: 'json',
data: "test",
success:function(data) {
alert(data.status);
},
complete: function(data){
alert("complete")
}
});
});
});
关于javascript - 如何向 Drupal 6 中的特定模块发送 ajax post 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8171538/