我创建了一个python文件,它从MySQL db中获取数据并使用' json.dumps'将其编码为JSON格式。
我需要使用ajax和jQuery在我的浏览器上显示这些数据。 但是,我无法理解如何在JavaScript代码中获取数据,然后根据我的意愿在我创建的Google地图上使用数据。
请帮我解释一下代码。
我附加了python和html代码。
Python代码
#!/usr/local/bin/python
import MySQLdb
import json
db = MySQLdb.connect (host = "localhost", user = "root", passwd = "***", db = "client_database")
cursor = db.cursor ()
cursor.execute ("select FULL_NAME, EMAIL_ID, MOBILE, GENDER, AREA_OF_EXPERTISE, YEARS_OF_EXPERIENCE, LATITUDE, LONGITUDE from client_info")
data = cursor.fetchall()
display = json.dumps(data, indent=4)
cursor.close ()
db.close ()
HTML代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta charset="utf-8" />
<title>DevMap</title>
<style>
#map {
width: 100%;
height: 100%;
}
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#description {
font-family: Calibri;
font-size: 15px;
font-weight: 300;
}
#infowindow-content.title {
display: none;
}
#map #infowindow-content {
display: none;
}
.pac-card {
margin: 10px 10px 0 0;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
background-color: #fff;
font-family: Calibri;
}
#pac-container {
padding-bottom: 12px;
margin-right: 12px;
}
.pac-controls {
display: inline-block;
padding: 5px 11px;
}
.pac-controls label {
font-family: Calibri;
font-size: 16px;
font-weight: 400;
}
#pac-input {
background-color: white;
font-family: Calibri;
font-size: 18px;
margin-left: 12px;
padding: 15px 15px;
text-overflow: ellipsis;
width: 500px;
border: 1px solid black;
border-radius: 6px;
}
#pac-input:focus {
border-color: #00015E;
}
#title {
color: #fff;
background-color: #4d90fe;
font-size: 25px;
font-weight: 400;
padding: 6px 12px;
}
#target {
width: 345px;
}
#form {
overflow: scroll;
display: none;
}
#heading {
text-align: center;
color: #003366;
font-size: 30px;
font-family: Calibri;
padding: 8px 12px;
}
label.field {
text-align: left;
font-weight: bolder;
font-size: 16px;
font-family: Calibri;
}
input[type=text], input[type=email], input[type=number], select {
font-family: Calibri;
font-size: 16px;
font-weight: bolder;
padding: 8px 12px;
width: 100%;
margin: 8px 0;
display: inline-block;
border: 1px solid black;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit] {
font-family: Calibri;
font-size: 16px;
font-weight: bolder;
background-color: white;
color: #00A200;
padding: 8px 12px;
margin: 8px 0;
border: 2px solid #00A200;
border-radius: 4px;
cursor: pointer;
box-sizing: border-box;
}
input[type=submit]:hover {
background-color: #00A200;
color: white;
}
input[type=reset] {
font-family: Calibri;
font-size: 16px;
font-weight: bolder;
background-color: white;
color: #cc0000;
padding: 8px 12px;
margin: 8px 0;
border: 2px solid #cc0000;
border-radius: 4px;
cursor: pointer;
box-sizing: border-box;
}
input[type=reset]:hover {
background-color: #cc0000;
color: white;
}
input[type=text]:focus {
background-color: #f2f2f2;
border-color: #4d90fe;
}
input[type=email]:focus {
background-color: #f2f2f2;
border-color: #4d90fe;
}
input[type=number]:focus {
background-color: #f2f2f2;
border-color: #4d90fe;
}
select:focus {
background-color: #f2f2f2;
border-color: #4d90fe;
}
#overlay {
position: fixed;
display: none;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0,0,0,0.85);
z-index: 1;
overflow: hidden;
}
.box {
/* Linear gradient - By default left to right - (direction, color1, color2) */
background: -webkit-linear-gradient(left, aqua, royalblue); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient(right, aqua, royalblue); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient(right, aqua, royalblue); /* For Firefox 3.6 to 15 */
background: linear-gradient(to right, aqua , royalblue); /* Standard syntax */
background-color: #F1C40F;
text-align: center;
font-family: Calibri;
color: midnightblue;
font-size: 60px;
font-weight:bold;
margin:200px auto;
height:70px;
width: 100%;
padding-top: 10px;
padding-bottom: 10px;
overflow: hidden;
position: relative;
}
.button {
overflow: hidden;
cursor: pointer;
background-color: #FF0000;
border: 0.5px solid;
border-color: #000000;
border-radius: 6px;
width: 160px;
color: white;
text-align: center;
text-decoration: none;
padding: 10px 10px;
display: inline-block;
font-family: Calibri;
font-size: 17px;
font-weight: bold;
position: relative;
}
.button:hover {
background-color: #AB0000;
}
.wrapper {
text-align: center;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?key=********************&libraries=places&callback=initAutocomplete"
async defer>
</script>
</head>
<body onload="on()">
<!-- Overlay with logo. -->
<div id="overlay">
<div class="box">DevMap</div>
<!-- Register/Find Developer button. -->
<div class="wrapper">
<button class="button" onclick="off()">Register Yourself!</button>
<button class="button" onclick="off()">Find Developers!</button>
</div>
</div>
<!-- Creating form to display inside infowindow. -->
<div id="map"></div>
<input id="pac-input" class="controls" type="text" placeholder="Enter your location here..." />
<div id="form">
<form action="/cgi-bin/processing.py" method="post">
<p id="heading">PERSONAL INFORMATION</p>
<label class="field" for="fullname"><b>Full Name:</b></label>
<input type="text" name="fullname" placeholder="Eg. Suraj Makhija" size="30" required />
<br />
<br />
<label class="field" for="emailid"><b>E-mail:</b></label>
<input type="email" name="emailid" placeholder="Eg. contact@domain.com" size="30" required />
<br />
<br />
<label class="field" for="mobile"><b>Mobile:</b></label>
<input type="text" name="mobile" placeholder="Eg. 9999999999" maxlength="10" minlength="10" size="30" required />
<br />
<br />
<label class="field" for="gender"><b>Gender:</b></label>
<select name="gender" required>
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
<br />
<br />
<label class="field" for="areaofexpertise"><b>Area of Expertise:</b></label>
<input type="text" name="areaofexpertise" placeholder="Eg. Android/iOS Developer" size="30" required />
<br />
<br />
<label class="field" for="yearsofexperience"><b>Years of Experience:</b></label>
<input type="number" name="yearsofexperience" placeholder="Eg. 10" min="0" max="100" required />
<br />
<br />
<input type="hidden" name="location_lat" id="lat">
<input type="hidden" name="location_lng" id="lng">
<p style="text-align: center;">
<input type="submit" value="Submit" />
<input type="reset" value="Clear All" />
</p>
</form>
</div>
<script>
var map;
var iconBase;
var input;
var searchBox;
var markers=[];
var markerArray =[];
var infowindow;
//Declaring different markers for male/female.
var customIcon = {
male: {
iconBase : 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
},
female: {
iconBase : 'http://labs.google.com/ridefinder/images/mm_20_purple.png'
}
};
//Overlay function.
function on() {
document.getElementById("overlay").style.display = "block";
}
function off() {
document.getElementById("overlay").style.display = "none";
}
//Declaring map function.
function initAutocomplete() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 20.5937, lng: 78.9629},
zoom: 5,
mapTypeId: 'roadmap',
gestureHandling: 'cooperative'
});
//Declaring infowindow.
infowindow = new google.maps.InfoWindow({
content: document.getElementById('form')
});
// Create the search box and link it to the UI element.
input = document.getElementById('pac-input');
searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_CENTER].push(input);
// Bias the SearchBox results towards current map's viewport.
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
//Creates a marker when the user clicks on a location on the map.
google.maps.event.addListener(map, 'click', function(event) {
addMarker(event.latLng, map);
});
// Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
// Clear out the old markers.
markers.forEach(function(marker) {
marker.setMap(null);
});
// For each place, get the icon, name and location.
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
//Marker function declaration.
function addMarker(location, map) {
// Add the marker at the clicked location.
var iconBase = 'https://maps.google.com/mapfiles/kml/paddle/';
marker = new google.maps.Marker({
draggable: true,
animation: google.maps.Animation.DROP,
position: location,
map: map,
icon: iconBase + 'ylw-blank.png'
});
//Infowindow pops up on clicking the marker.
google.maps.event.addListener(marker, 'click', function() {
document.getElementById('form').style.display='inline-block';
infowindow.open(map, marker);
});
var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();
document.getElementById('lat').value=lat;
document.getElementById('lng').value=lng;
}
//Trying AJAX to run
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
myObj = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = myObj.FULL_NAME;
}
};
xmlhttp.open("GET", "/cgi-bin/display.py", true);
xmlhttp.send();
</script>
</body>