Mysql 外部域未通过ajax请求传递值
我使用CPanel的服务器,我的手机应用程序的配置文件中有域名,但当我将索引和所有组件(Javascripts、PHP文件、SCS、CSS)上传到服务器和Phonegap并在手机上下载时,它不会检索MySQL表的数据。它有时会显示本地机器和移动设备上的业务下拉列表,但不会显示存储在第二个表中的标记。我想我需要在配置文件中添加插件,但是我不确定是哪些插件。我还需要让AJAX或JSON正常工作。目前我添加了access origin=“my domain name”,但它仍然不想从服务器上完全检索信息。它检索所有其他组件,但是这个PHP似乎缺少它的功能。(注:当从服务器打开应用程序时,该应用程序工作正常,但不能在本地计算机上运行)。以下是我的代码: AJAX+JavaScriptMysql 外部域未通过ajax请求传递值,mysql,cordova,phonegap-plugins,config,cpanel,Mysql,Cordova,Phonegap Plugins,Config,Cpanel,我使用CPanel的服务器,我的手机应用程序的配置文件中有域名,但当我将索引和所有组件(Javascripts、PHP文件、SCS、CSS)上传到服务器和Phonegap并在手机上下载时,它不会检索MySQL表的数据。它有时会显示本地机器和移动设备上的业务下拉列表,但不会显示存储在第二个表中的标记。我想我需要在配置文件中添加插件,但是我不确定是哪些插件。我还需要让AJAX或JSON正常工作。目前我添加了access origin=“my domain name”,但它仍然不想从服务器上完全检索信
<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>
src=”http://maps.googleapis.com/maps/api/js">
/*调用包含json_编码的php数组的php*/
$.ajax({
网址:'http://ct5006-14h.studentsites.glos.ac.uk/FinalProject/cheltenham.php',
键入:“GET”,
数据类型:“jsonp”,
jsonp:'jsoncallback',
跨域:是的,
成功:功能(数据、状态){
警报(“ajax成功”);
/*调用包含json_编码的php数组的php*/
//$.getJSON(“”,函数(数据){
/*数据将把php数组作为javascript对象保存*/
$。每个(数据、函数(键、值){
var x=document.getElementById(“切尔滕纳姆”);
var option=document.createElement(“选项”);
option.text=val.BUSTYPE;
option.value=val.REFERENCE;
x、 添加(选项);
});
//});
},
错误:功能(请求、状态、错误){
警报(状态);
}
});
$(文档).on('click','#Cheltenham',function(){
var x=document.getElementById(“切尔滕纳姆”);
var referenceID=x.options[x.selectedIndex].value;
log(“referenceID:+referenceID”);
日志(“测试:+referenceID”);
var mapCanvas=document.getElementById('myMap');
变量映射选项={
中心:新google.maps.LatLng(51.8979988098144,-2.083859920517),
缩放:12,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var map=new google.maps.map(mapCanvas,mapOptions);
$.getJSON('searchBusinesss.php?REFERENCE='+referenceID,函数(数据){
/*数据将把php数组作为javascript对象保存*/
/*数据将把php数组作为javascript对象保存*/
$。每个(数据、函数(键、值){
var v_lati=瓦尔纬度;
var v_longi=经度值;
var mylatng=new google.maps.LatLng(v_lati,v_longi);
console.log(“LatLongNew:+myLatlng”);
var marker=new google.maps.marker({
地图:地图,
图标:“images/marker.png”,
职位:myLatlng,
动画:google.maps.animation.DROP,
可拖动:错误,
});
var contentString=''+val.NAME;
var infowindow=new google.maps.infowindow({
内容:contentString
});
google.maps.event.addListener(标记'click',函数(){
信息窗口。打开(地图、标记);
});
});
});
log(“test1:+referenceID”);
google.maps.event.trigger(映射,'resize');
});
函数初始化(){
var mapCanvas=document.getElementById('myMap');
变量映射选项={
中心:新google.maps.LatLng(51.8979988098144,-2.083859920517),
缩放:12,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
var map=new google.maps.map(mapCanvas,mapOptions)
}
google.maps.event.addDomListener(窗口“加载”,初始化);
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();
?>
searchbusiness.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();
?>
我使用了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);
我必须用它吗?我不知道这是我第一次做这种事情我需要添加什么样的Ajax代码?我应该将它包含在seprate文件中还是索引文件中?我使用了json,我认为它是http json还是Ajax请求并不重要,只要我有它,我的朋友。