Drupal 7 ajax工作不正常(调用了成功函数,但打印的是Drupal页面,而不是页面回调结果)


Drupal 7 ajax not working properly (success function called but the drupal page is being printed instead of the page callbacks results)

你好,我在drupal 7中使用ajax从地图的边界获取数据。将其序列化为字符串并作为post提交到服务器,作为视图的过滤器添加,以使显示对缩放和数组边界的更改做出响应。

成功回调被调用,但返回的数据不是页面回调函数的结果,firebug中的xhtml响应是整个网页。我真的很感激知道是什么阻止了页面回调函数值的返回。

下面是javascript代码:

    (function ($) {
    Drupal.behaviors.mapresults = {
    attach: function() {
  // Dynamically show  the marker nodes
     // first find the right map
    $.each(Drupal.settings.getlocations, function (key, settings) {
    // this is the one we want
    alert("0");
    if (1 == 1) {
      // an event handler on map zoom to check if the view results needs to be updated     
      on zoom 
      // hooking another event and checking zoom may work better.
      google.maps.event.addListener(getlocations_map[key], 'bounds_changed', function()
      {
        // count number of markers <= 10'
        // global_settings.mgr
        //mgr = new MarkerManager(getlocations_map[key]);
         //zoomcount = mgr.getMarkerCount(getlocations_map[key].getZoom());
         zoomcount = 10;
         if(zoomcount < 100){
             // get bounds
             marker_bounds = getlocations_map[key].getBounds();

         // calculate horizontal distance from bounds to get max  and min latitude and
         longitude for the for box
        // submit data with jquery get bounds etc.
        var marker_bounds_string = JSON.stringify(marker_bounds);
        // ajax link to submit data by post
        $.ajax({
            type: 'POST',
            url: 'ajax/mapresults',
            dataType: "json",                   
            data : {marker_bounds : marker_bounds_string},             
            success: function (data)    
     {document.getElementById('map_results').innerHTML = "success";},
            error: function (xmlhttp)  
     {document.getElementById('map_results').innerHTML = xmlhttp.status;}
            });

        }
         else{
         }
      });
    }
  });
}
};
}(jQuery));

这是drupal模块代码:

    <?php
function mapresults_menu() {
  $items['ajax/mapresults'] = array(
    'title' => t('mapresults AJAX'),
    'type' => MENU_CALLBACK,
    'page callback' => 'mapresults_ajax',
    'access arguments' => array('access content'),
  );
  return $items;
}
function mapresults_ajax() {
// call back for display map options
drupal_exit();
$view = views_get_view('search');
// set display
$display_id = 'default';
// add filters from ajax
// float
$bounds_array = json_decode($_POST['marker_bounds'])
//get cordinates from bounds_array
$lat_min = $bounds_array[0][0];
$lat_max = $bounds_array[1][0];
$lon_min  = $bounds_array[0][1];
$lon_max = $bounds_array[1][1];
// add filters to view
$view->exposed_input['ubicacion_latitiude'] = $lat_min;
$view->exposed_input['ubicacion_latitiude_1'] = $lat_max;
$view->exposed_input['ubicacion_longitude'] = $lon_min;
$view->exposed_input['ubicacion_longitude_1'] = $lon_max;
$view->pre_execute();
$view->execute();
// display ajax results in container
$data = $view->preview($display_id);
$results_count = count($view->result);
if($results_count <= 10){
    // Return json      
    drupal_json_output($data);//
drupal_exit();
}
else{
    drupal_json_output("ih");
drupal_exit();
    }
}
?>

mapresults_ajax没有返回任何内容。在这种情况下,我将使用print而不是return将结果发送回javascript,而不返回整个HTML页面。

将drupal_json_output更改为打印drupal_6json_output