我使用CPanel的服务器,我在我的移动应用程序的配置文件中有域名,但是当我将索引和所有组件(Javascripts,PHP文件,SCSS,CSS)上传到服务器和Phonegap并下载它时在我的手机上,它不会检索MySQL表的数据。它有时会显示本地计算机和移动设备上的企业下拉列表,但不会显示存储在第二个表中的标记。我想我需要在Config文件中添加插件,但我不知道哪些插件。我还需要让AJAX或JSON正常工作。目前我添加了access origin ="我的域名"但它仍然不想完全从服务器检索信息。它检索所有其他组件,但这个PHP似乎缺乏其功能。 (PS。当从服务器打开但在本地计算机上打开时,应用程序完全正常)。这是我的代码:
AJAX + JavaScript
<div id="Manu">
<div class="Cheltenham">
<select id="Cheltenham" class="select" data-theme="d"></select>
</div>
<!--Map-->
<div id="myMap">
<script>src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
/* call the php that has the php array which is json_encoded */
$.ajax({
url: 'http://ct5006-14h.studentsites.glos.ac.uk/FinalProject/cheltenham.php',
type: 'GET',
dataType: "jsonp",
jsonp: 'jsoncallback',
crossDomain: true,
success: function (data, status) {
alert("ajax success");
/* call the php that has the php array which is json_encoded */
//$.getJSON('', function(data){
/* data will hold the php array as a javascript object */
$.each(data, function(key, val){
var x = document.getElementById("Cheltenham");
var option = document.createElement("option");
option.text = val.BUSTYPE;
option.value = val.REFERENCE;
x.add(option);
});
//});
},
error: function (request, status, error) {
alert(status);
}
});
</script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script>
$(document).on('click', '#Cheltenham', function() {
var x = document.getElementById("Cheltenham");
var referenceID = x.options[x.selectedIndex].value;
console.log("referenceID:" + referenceID);
console.log("test:" + referenceID);
var mapCanvas = document.getElementById('myMap');
var mapOptions = {
center: new google.maps.LatLng(51.8979988098144,-2.0838599205017),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions);
$.getJSON('SearchBusinesses.php?REFERENCE='+referenceID, function(data){
/* data will hold the php array as a javascript object */
/* data will hold the php array as a javascript object */
$.each(data, function(key, val){
var v_lati = val.LATITUDE;
var v_longi = val.LONGITUDE;
var myLatlng = new google.maps.LatLng(v_lati,v_longi);
console.log("LatLongNew:" + myLatlng);
var marker = new google.maps.Marker({
map: map,
icon: 'images/marker.png',
position: myLatlng,
animation: google.maps.Animation.DROP,
draggable: false,
});
var contentString = '' + val.NAME;
var infowindow = new google.maps.InfoWindow({
content: contentString
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
});
});
console.log("test1:" + referenceID);
google.maps.event.trigger(map, 'resize');
});
</script>
<script>
function initialize() {
var mapCanvas = document.getElementById('myMap');
var mapOptions = {
center: new google.maps.LatLng(51.8979988098144,-2.0838599205017),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(mapCanvas, mapOptions)
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</div>
</div>
cheltenham.php
<?php
$servername = "localhost";
$username = "...";
$password = "...";
$dbname = "...";
function getEventList(){
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM `BUSINESS_TYPE`";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
} else {
//echo "0 results" . "<br>";
}
echo $_GET ['jsoncallback'].'('. json_encode($rows).');';
$conn->close();
}
getEventList();
?>
SearchBusinesses.php
<?php
$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";
$evReference = $_GET['REFERENCE'];
function getEventOne(){
global $servername, $username, $password, $dbname, $evReference;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `LATITUDE`, `LONGITUDE`, `NAME` FROM `BUSINESSES` where `REFERENCE` = '" . $evReference . "'";
$result = $conn->query($sql);
$rows = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
} else {
//echo "0 results" . "<br>";
}
echo json_encode($rows);
$conn->close();
}
getEventOne();
?>
答案 0 :(得分:0)
我使用JSON并在php中添加了一行来强制JSON传递值
$.getJSON('http://...', function(data){
/* data will hold the php array as a javascript object */
$.each(data, function(key, val){
var x = document.getElementById("Cheltenham");
var option = document.createElement("option");
option.text = val.BUSTYPE;
option.value = val.REFERENCE;
x.add(option);
});
});
在我的chelteham.php中,我添加了这个并且它有效:
echo json_encode($rows, JSON_FORCE_OBJECT);