您好,我在 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();
}
}
?>