Drupal 7 ajax无法正常工作(调用成功函数但正在打印drupal页面而不是页面回调结果)

时间:2013-07-31 23:59:37

标签: php jquery ajax json drupal

您好我在drupal 7中使用ajax从地图的边界获取数据将其序列化为字符串并作为帖子提交给服务器以作为视图的过滤器添加以使显示响应变焦和数组边界。

调用成功回调,但返回的数据不是页面回调函数的结果,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();
    }
}
?>

1 个答案:

答案 0 :(得分:0)

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

更改你的drupal_json_output以打印drupal_json_output