Javascript MySQL中的Ajax调用比MS Sql慢
我在一个带有标记的网站上显示地图。我使用servlet从mysql数据库获取位置,并在js中获取Javascript MySQL中的Ajax调用比MS Sql慢,javascript,mysql,ajax,Javascript,Mysql,Ajax,我在一个带有标记的网站上显示地图。我使用servlet从mysql数据库获取位置,并在js中获取 function initMap() { getLocationFromServlet(xhr); //get setTimeout(function() { createMap(); //create map after getting coord from getLocationFromServlet(xhr), but latArray is emp
function initMap() {
getLocationFromServlet(xhr); //get
setTimeout(function() {
createMap(); //create map after getting coord from getLocationFromServlet(xhr), but latArray is empty
console.log(latArray); //empty here though defined in getLocationFromServlet
}, 1300);
}
function createMap() {
var mapDiv = document.getElementById('map');
map = new google.maps.Map(mapDiv);
map.setCenter(new google.maps.LatLng(latArray[0], lngArray[0]));
map.setZoom(17);
createMarker();
setTimeout(function() {
console.log(latArray);
}, 1600)
}
function createMarker() {
latArray.forEach(function(lat, i) {
var infoWindow = new google.maps.InfoWindow({
content: '<div id="content>' + '<p style="color:#000000">DeviceID:<p>' + '<p>' + deviceId[i] + '</p>' + '</div>'
});
var marker = new google.maps.Marker({
map: map,
mapTypeId: google.maps.MapTypeId.ROADMAP,
//position: new google.maps.LatLng(latArray[i], lngArray[i]),
icon: "phone6.png"
});
markerArray.push(marker);
markerArray[i].setPosition(new google.maps.LatLng(latArray[i], lngArray[i]));
markerArray[i].addListener("click", function() {
infoWindow.open(map, marker);
});
});
console.log(latArray);
}
function getLocationFromServlet(xhr) {
xhr = new XMLHttpRequest();
xhr.open('POST', 'GetLocationFromDB', true);
xhr.send();
xhr.onreadystatechange = function() {
console.log(xhr.readyState == 4); // false twice in console.log, but I call the function only once
if (xhr.readyState == 4) {
data = JSON.parse(xhr.responseText);
//console.log(data);
if (latArrayCount != 1) {
latArray.length = 0;
lngArray.length = 0;
deviceId.length = 0;
} else {
latArrayCount++;
}
for (i = 0; i < data.length; i++) {
if (!((i + 1) % 3 == 0)) {
//console.log(data);
latArray.push(data[i]);
lngArray.push(data[i + 1]);
deviceId.push(data[i + 2]);
i = i + 2;
console.log("lat" + latArray);
}
}
}
}
console.log(latArray); //displayed empty twice before displaying latArray values
}
函数initMap(){
getLocationFromServlet(xhr);//获取
setTimeout(函数(){
createMap();//从getLocationFromServlet(xhr)获取坐标后创建映射,但latArray为空
console.log(latArray);//此处为空,但在getLocationFromServlet中定义
}, 1300);
}
函数createMap(){
var mapDiv=document.getElementById('map');
map=新的google.maps.map(mapDiv);
setCenter(新的google.maps.LatLng(latArray[0],lngArray[0]);
map.setZoom(17);
createMarker();
setTimeout(函数(){
console.log(latArray);
}, 1600)
}
函数createMarker(){
latArray.forEach(函数(lat,i){
var infoWindow=new google.maps.infoWindow({
内容:'这些代码都与MS SQL或MySQL无关…@David。它在MS中如何工作SQL@boo:这听起来像是您需要以某种方式进行调试和检查的事情。可能您的服务器端代码表现不同,可能您的查询表现不同,可能您的数据库结构不同,等等。但是如果您如果服务器端资源出现问题,只查看浏览器中的客户端代码不会让您走得很远。@Andy好的。.但我无法创建地图这些代码都与MS SQL或MySQL无关…@David.在MS中如何工作SQL@boo:这听起来像是你需要调试和检查的东西。也许你的服务er端代码的行为有所不同,可能您的查询行为有所不同,可能您的数据库结构有所不同,等等。但如果您在服务器端资源方面遇到问题,仅在浏览器中查看客户端代码不会让您走得很远。@Andy好的。。但我无法创建映射